]> git.sur5r.net Git - openldap/blob - servers/slapd/bconfig.c
Integer indexing - handle huge values
[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-2007 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 #define CONFIG_RDN      "cn=config"
41 #define SCHEMA_RDN      "cn=schema"
42
43 static struct berval config_rdn = BER_BVC(CONFIG_RDN);
44 static struct berval schema_rdn = BER_BVC(SCHEMA_RDN);
45
46 extern int slap_DN_strict;      /* dn.c */
47
48 #ifdef SLAPD_MODULES
49 typedef struct modpath_s {
50         struct modpath_s *mp_next;
51         struct berval mp_path;
52         BerVarray mp_loads;
53 } ModPaths;
54
55 static ModPaths modpaths, *modlast = &modpaths, *modcur = &modpaths;
56 #endif
57
58 typedef struct ConfigFile {
59         struct ConfigFile *c_sibs;
60         struct ConfigFile *c_kids;
61         struct berval c_file;
62         AttributeType *c_at_head, *c_at_tail;
63         ContentRule *c_cr_head, *c_cr_tail;
64         ObjectClass *c_oc_head, *c_oc_tail;
65         OidMacro *c_om_head, *c_om_tail;
66         BerVarray c_dseFiles;
67 } ConfigFile;
68
69 typedef struct {
70         ConfigFile *cb_config;
71         CfEntryInfo *cb_root;
72         BackendDB       cb_db;  /* underlying database */
73         int             cb_got_ldif;
74         int             cb_use_ldif;
75 } CfBackInfo;
76
77 static CfBackInfo cfBackInfo;
78
79 static char     *passwd_salt;
80 static FILE *logfile;
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, *cfAd_attr, *cfAd_oc, *cfAd_om;
91
92 static ConfigFile *cfn;
93
94 static Avlnode *CfOcTree;
95
96 /* System schema state */
97 extern AttributeType *at_sys_tail;      /* at.c */
98 extern ObjectClass *oc_sys_tail;        /* oc.c */
99 extern OidMacro *om_sys_tail;   /* oidm.c */
100 static AttributeType *cf_at_tail;
101 static ObjectClass *cf_oc_tail;
102 static OidMacro *cf_om_tail;
103
104 static int config_add_internal( CfBackInfo *cfb, Entry *e, ConfigArgs *ca,
105         SlapReply *rs, int *renumber, Operation *op );
106
107 static int config_check_schema( Operation *op, CfBackInfo *cfb );
108
109 static ConfigDriver config_fname;
110 static ConfigDriver config_cfdir;
111 static ConfigDriver config_generic;
112 static ConfigDriver config_search_base;
113 static ConfigDriver config_passwd_hash;
114 static ConfigDriver config_schema_dn;
115 static ConfigDriver config_sizelimit;
116 static ConfigDriver config_timelimit;
117 static ConfigDriver config_overlay;
118 static ConfigDriver config_subordinate; 
119 static ConfigDriver config_suffix; 
120 static ConfigDriver config_rootdn;
121 static ConfigDriver config_rootpw;
122 static ConfigDriver config_restrict;
123 static ConfigDriver config_allows;
124 static ConfigDriver config_disallows;
125 static ConfigDriver config_requires;
126 static ConfigDriver config_security;
127 static ConfigDriver config_referral;
128 static ConfigDriver config_loglevel;
129 static ConfigDriver config_updatedn;
130 static ConfigDriver config_updateref;
131 static ConfigDriver config_include;
132 static ConfigDriver config_obsolete;
133 #ifdef HAVE_TLS
134 static ConfigDriver config_tls_option;
135 static ConfigDriver config_tls_config;
136 #endif
137 extern ConfigDriver syncrepl_config;
138
139 enum {
140         CFG_ACL = 1,
141         CFG_BACKEND,
142         CFG_DATABASE,
143         CFG_TLS_RAND,
144         CFG_TLS_CIPHER,
145         CFG_TLS_CERT_FILE,
146         CFG_TLS_CERT_KEY,
147         CFG_TLS_CA_PATH,
148         CFG_TLS_CA_FILE,
149         CFG_TLS_DH_FILE,
150         CFG_TLS_VERIFY,
151         CFG_TLS_CRLCHECK,
152         CFG_TLS_CRL_FILE,
153         CFG_CONCUR,
154         CFG_THREADS,
155         CFG_SALT,
156         CFG_LIMITS,
157         CFG_RO,
158         CFG_REWRITE,
159         CFG_DEPTH,
160         CFG_OID,
161         CFG_OC,
162         CFG_DIT,
163         CFG_ATTR,
164         CFG_ATOPT,
165         CFG_ROOTDSE,
166         CFG_LOGFILE,
167         CFG_PLUGIN,
168         CFG_MODLOAD,
169         CFG_MODPATH,
170         CFG_LASTMOD,
171         CFG_AZPOLICY,
172         CFG_AZREGEXP,
173         CFG_SASLSECP,
174         CFG_SSTR_IF_MAX,
175         CFG_SSTR_IF_MIN,
176         CFG_TTHREADS,
177         CFG_MIRRORMODE,
178         CFG_HIDDEN,
179         CFG_MONITORING,
180         CFG_SERVERID,
181         CFG_SORTVALS,
182         CFG_IX_INTLEN,
183
184         CFG_LAST
185 };
186
187 typedef struct {
188         char *name, *oid;
189 } OidRec;
190
191 static OidRec OidMacros[] = {
192         /* OpenLDAProot:666.11.1 */
193         { "OLcfg", "1.3.6.1.4.1.4203.666.11.1" },
194         { "OLcfgAt", "OLcfg:3" },
195         { "OLcfgGlAt", "OLcfgAt:0" },
196         { "OLcfgBkAt", "OLcfgAt:1" },
197         { "OLcfgDbAt", "OLcfgAt:2" },
198         { "OLcfgOvAt", "OLcfgAt:3" },
199         { "OLcfgCtAt", "OLcfgAt:4" },   /* contrib modules */
200         { "OLcfgOc", "OLcfg:4" },
201         { "OLcfgGlOc", "OLcfgOc:0" },
202         { "OLcfgBkOc", "OLcfgOc:1" },
203         { "OLcfgDbOc", "OLcfgOc:2" },
204         { "OLcfgOvOc", "OLcfgOc:3" },
205         { "OLcfgCtOc", "OLcfgOc:4" },   /* contrib modules */
206
207         /* Syntaxes. We should just start using the standard names and
208          * document that they are predefined and available for users
209          * to reference in their own schema. Defining schema without
210          * OID macros is for masochists...
211          */
212         { "OMsyn", "1.3.6.1.4.1.1466.115.121.1" },
213         { "OMsBoolean", "OMsyn:7" },
214         { "OMsDN", "OMsyn:12" },
215         { "OMsDirectoryString", "OMsyn:15" },
216         { "OMsIA5String", "OMsyn:26" },
217         { "OMsInteger", "OMsyn:27" },
218         { "OMsOID", "OMsyn:38" },
219         { "OMsOctetString", "OMsyn:40" },
220         { NULL, NULL }
221 };
222
223 /*
224  * Backend/Database registry
225  *
226  * OLcfg{Bk|Db}{Oc|At}:0                -> common
227  * OLcfg{Bk|Db}{Oc|At}:1                -> back-bdb(/back-hdb)
228  * OLcfg{Bk|Db}{Oc|At}:2                -> back-ldif
229  * OLcfg{Bk|Db}{Oc|At}:3                -> back-ldap
230  * OLcfg{Bk|Db}{Oc|At}:4                -> back-monitor
231  * OLcfg{Bk|Db}{Oc|At}:5                -> back-relay
232  * OLcfg{Bk|Db}{Oc|At}:6                -> back-sql
233  */
234
235 /*
236  * Overlay registry
237  *
238  * OLcfgOv{Oc|At}:1                     -> syncprov
239  * OLcfgOv{Oc|At}:2                     -> pcache
240  * OLcfgOv{Oc|At}:3                     -> chain
241  * OLcfgOv{Oc|At}:4                     -> accesslog
242  * OLcfgOv{Oc|At}:5                     -> valsort
243  * OLcfgOv{Oc|At}:7                     -> distproc
244  * OLcfgOv{Oc|At}:8                     -> dynlist
245  * OLcfgOv{Oc|At}:9                     -> dds
246  * OLcfgOv{Oc|At}:10                    -> unique
247  * OLcfgOv{Oc|At}:11                    -> refint
248  * OLcfgOv{Oc|At}:12                    -> ppolicy
249  * OLcfgOv{Oc|At}:13                    -> constraint
250  * OLcfgOv{Oc|At}:14                    -> translucent
251  * OLcfgOv{Oc|At}:15                    -> auditlog
252  * OLcfgOv{Oc|At}:16                    -> rwm
253  * OLcfgOv{Oc|At}:17                    -> dyngroup
254  * OLcfgOv{Oc|At}:18                    -> memberof
255  * OLcfgOv{Oc|At}:19                    -> collect
256  */
257
258 /* alphabetical ordering */
259
260 static ConfigTable config_back_cf_table[] = {
261         /* This attr is read-only */
262         { "", "", 0, 0, 0, ARG_MAGIC,
263                 &config_fname, "( OLcfgGlAt:78 NAME 'olcConfigFile' "
264                         "DESC 'File for slapd configuration directives' "
265                         "EQUALITY caseIgnoreMatch "
266                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
267         { "", "", 0, 0, 0, ARG_MAGIC,
268                 &config_cfdir, "( OLcfgGlAt:79 NAME 'olcConfigDir' "
269                         "DESC 'Directory for slapd configuration backend' "
270                         "EQUALITY caseIgnoreMatch "
271                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
272         { "access",     NULL, 0, 0, 0, ARG_MAY_DB|ARG_MAGIC|CFG_ACL,
273                 &config_generic, "( OLcfgGlAt:1 NAME 'olcAccess' "
274                         "DESC 'Access Control List' "
275                         "EQUALITY caseIgnoreMatch "
276                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
277         { "allows",     "features", 2, 0, 5, ARG_PRE_DB|ARG_MAGIC,
278                 &config_allows, "( OLcfgGlAt:2 NAME 'olcAllows' "
279                         "DESC 'Allowed set of deprecated features' "
280                         "EQUALITY caseIgnoreMatch "
281                         "SYNTAX OMsDirectoryString )", NULL, NULL },
282         { "argsfile", "file", 2, 2, 0, ARG_STRING,
283                 &slapd_args_file, "( OLcfgGlAt:3 NAME 'olcArgsFile' "
284                         "DESC 'File for slapd command line options' "
285                         "EQUALITY caseIgnoreMatch "
286                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
287         { "attributeoptions", NULL, 0, 0, 0, ARG_MAGIC|CFG_ATOPT,
288                 &config_generic, "( OLcfgGlAt:5 NAME 'olcAttributeOptions' "
289                         "EQUALITY caseIgnoreMatch "
290                         "SYNTAX OMsDirectoryString )", NULL, NULL },
291         { "attribute",  "attribute", 2, 0, STRLENOF( "attribute" ),
292                 ARG_PAREN|ARG_MAGIC|CFG_ATTR,
293                 &config_generic, "( OLcfgGlAt:4 NAME 'olcAttributeTypes' "
294                         "DESC 'OpenLDAP attributeTypes' "
295                         "EQUALITY caseIgnoreMatch "
296                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )",
297                                 NULL, NULL },
298         { "authid-rewrite", NULL, 2, 0, STRLENOF( "authid-rewrite" ),
299 #ifdef SLAP_AUTH_REWRITE
300                 ARG_MAGIC|CFG_REWRITE|ARG_NO_INSERT, &config_generic,
301 #else
302                 ARG_IGNORED, NULL,
303 #endif
304                  "( OLcfgGlAt:6 NAME 'olcAuthIDRewrite' "
305                         "EQUALITY caseIgnoreMatch "
306                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
307         { "authz-policy", "policy", 2, 2, 0, ARG_STRING|ARG_MAGIC|CFG_AZPOLICY,
308                 &config_generic, "( OLcfgGlAt:7 NAME 'olcAuthzPolicy' "
309                         "EQUALITY caseIgnoreMatch "
310                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
311         { "authz-regexp", NULL, 3, 3, 0, ARG_MAGIC|CFG_AZREGEXP|ARG_NO_INSERT,
312                 &config_generic, "( OLcfgGlAt:8 NAME 'olcAuthzRegexp' "
313                         "EQUALITY caseIgnoreMatch "
314                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
315         { "backend", "type", 2, 2, 0, ARG_PRE_DB|ARG_MAGIC|CFG_BACKEND,
316                 &config_generic, "( OLcfgGlAt:9 NAME 'olcBackend' "
317                         "DESC 'A type of backend' "
318                         "EQUALITY caseIgnoreMatch "
319                         "SYNTAX OMsDirectoryString SINGLE-VALUE X-ORDERED 'SIBLINGS' )",
320                                 NULL, NULL },
321         { "concurrency", "level", 2, 2, 0, ARG_INT|ARG_MAGIC|CFG_CONCUR,
322                 &config_generic, "( OLcfgGlAt:10 NAME 'olcConcurrency' "
323                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
324         { "conn_max_pending", "max", 2, 2, 0, ARG_INT,
325                 &slap_conn_max_pending, "( OLcfgGlAt:11 NAME 'olcConnMaxPending' "
326                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
327         { "conn_max_pending_auth", "max", 2, 2, 0, ARG_INT,
328                 &slap_conn_max_pending_auth, "( OLcfgGlAt:12 NAME 'olcConnMaxPendingAuth' "
329                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
330         { "database", "type", 2, 2, 0, ARG_MAGIC|CFG_DATABASE,
331                 &config_generic, "( OLcfgGlAt:13 NAME 'olcDatabase' "
332                         "DESC 'The backend type for a database instance' "
333                         "SUP olcBackend SINGLE-VALUE X-ORDERED 'SIBLINGS' )", NULL, NULL },
334         { "defaultSearchBase", "dn", 2, 2, 0, ARG_PRE_BI|ARG_PRE_DB|ARG_DN|ARG_QUOTE|ARG_MAGIC,
335                 &config_search_base, "( OLcfgGlAt:14 NAME 'olcDefaultSearchBase' "
336                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
337         { "disallows", "features", 2, 0, 8, ARG_PRE_DB|ARG_MAGIC,
338                 &config_disallows, "( OLcfgGlAt:15 NAME 'olcDisallows' "
339                         "EQUALITY caseIgnoreMatch "
340                         "SYNTAX OMsDirectoryString )", NULL, NULL },
341         { "ditcontentrule",     NULL, 0, 0, 0, ARG_MAGIC|CFG_DIT|ARG_NO_DELETE|ARG_NO_INSERT,
342                 &config_generic, "( OLcfgGlAt:16 NAME 'olcDitContentRules' "
343                         "DESC 'OpenLDAP DIT content rules' "
344                         "EQUALITY caseIgnoreMatch "
345                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )",
346                         NULL, NULL },
347         { "gentlehup", "on|off", 2, 2, 0,
348 #ifdef SIGHUP
349                 ARG_ON_OFF, &global_gentlehup,
350 #else
351                 ARG_IGNORED, NULL,
352 #endif
353                 "( OLcfgGlAt:17 NAME 'olcGentleHUP' "
354                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
355         { "hidden", "on|off", 2, 2, 0, ARG_DB|ARG_ON_OFF|ARG_MAGIC|CFG_HIDDEN,
356                 &config_generic, "( OLcfgDbAt:0.17 NAME 'olcHidden' "
357                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
358         { "idletimeout", "timeout", 2, 2, 0, ARG_INT,
359                 &global_idletimeout, "( OLcfgGlAt:18 NAME 'olcIdleTimeout' "
360                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
361         { "include", "file", 2, 2, 0, ARG_MAGIC,
362                 &config_include, "( OLcfgGlAt:19 NAME 'olcInclude' "
363                         "SUP labeledURI )", NULL, NULL },
364         { "index_substr_if_minlen", "min", 2, 2, 0, ARG_INT|ARG_NONZERO|ARG_MAGIC|CFG_SSTR_IF_MIN,
365                 &config_generic, "( OLcfgGlAt:20 NAME 'olcIndexSubstrIfMinLen' "
366                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
367         { "index_substr_if_maxlen", "max", 2, 2, 0, ARG_INT|ARG_NONZERO|ARG_MAGIC|CFG_SSTR_IF_MAX,
368                 &config_generic, "( OLcfgGlAt:21 NAME 'olcIndexSubstrIfMaxLen' "
369                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
370         { "index_substr_any_len", "len", 2, 2, 0, ARG_INT|ARG_NONZERO,
371                 &index_substr_any_len, "( OLcfgGlAt:22 NAME 'olcIndexSubstrAnyLen' "
372                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
373         { "index_substr_any_step", "step", 2, 2, 0, ARG_INT|ARG_NONZERO,
374                 &index_substr_any_step, "( OLcfgGlAt:23 NAME 'olcIndexSubstrAnyStep' "
375                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
376         { "index_intlen", "len", 2, 2, 0, ARG_INT|ARG_MAGIC|CFG_IX_INTLEN,
377                 &config_generic, "( OLcfgGlAt:84 NAME 'olcIndexIntLen' "
378                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
379         { "lastmod", "on|off", 2, 2, 0, ARG_DB|ARG_ON_OFF|ARG_MAGIC|CFG_LASTMOD,
380                 &config_generic, "( OLcfgDbAt:0.4 NAME 'olcLastMod' "
381                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
382         { "limits", "limits", 2, 0, 0, ARG_DB|ARG_MAGIC|CFG_LIMITS,
383                 &config_generic, "( OLcfgDbAt:0.5 NAME 'olcLimits' "
384                         "EQUALITY caseIgnoreMatch "
385                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
386         { "localSSF", "ssf", 2, 2, 0, ARG_INT,
387                 &local_ssf, "( OLcfgGlAt:26 NAME 'olcLocalSSF' "
388                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
389         { "logfile", "file", 2, 2, 0, ARG_STRING|ARG_MAGIC|CFG_LOGFILE,
390                 &config_generic, "( OLcfgGlAt:27 NAME 'olcLogFile' "
391                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
392         { "loglevel", "level", 2, 0, 0, ARG_MAGIC,
393                 &config_loglevel, "( OLcfgGlAt:28 NAME 'olcLogLevel' "
394                         "EQUALITY caseIgnoreMatch "
395                         "SYNTAX OMsDirectoryString )", NULL, NULL },
396         { "maxDerefDepth", "depth", 2, 2, 0, ARG_DB|ARG_INT|ARG_MAGIC|CFG_DEPTH,
397                 &config_generic, "( OLcfgDbAt:0.6 NAME 'olcMaxDerefDepth' "
398                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
399         { "mirrormode", "on|off", 2, 2, 0, ARG_DB|ARG_ON_OFF|ARG_MAGIC|CFG_MIRRORMODE,
400                 &config_generic, "( OLcfgDbAt:0.16 NAME 'olcMirrorMode' "
401                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
402         { "moduleload", "file", 2, 0, 0,
403 #ifdef SLAPD_MODULES
404                 ARG_MAGIC|CFG_MODLOAD|ARG_NO_DELETE, &config_generic,
405 #else
406                 ARG_IGNORED, NULL,
407 #endif
408                 "( OLcfgGlAt:30 NAME 'olcModuleLoad' "
409                         "EQUALITY caseIgnoreMatch "
410                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
411         { "modulepath", "path", 2, 2, 0,
412 #ifdef SLAPD_MODULES
413                 ARG_MAGIC|CFG_MODPATH|ARG_NO_DELETE|ARG_NO_INSERT, &config_generic,
414 #else
415                 ARG_IGNORED, NULL,
416 #endif
417                 "( OLcfgGlAt:31 NAME 'olcModulePath' "
418                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
419         { "monitoring", "TRUE|FALSE", 2, 2, 0,
420                 ARG_MAGIC|CFG_MONITORING|ARG_DB|ARG_ON_OFF, &config_generic,
421                 "( OLcfgDbAt:0.18 NAME 'olcMonitoring' "
422                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
423         { "objectclass", "objectclass", 2, 0, 0, ARG_PAREN|ARG_MAGIC|CFG_OC,
424                 &config_generic, "( OLcfgGlAt:32 NAME 'olcObjectClasses' "
425                 "DESC 'OpenLDAP object classes' "
426                 "EQUALITY caseIgnoreMatch "
427                 "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )",
428                         NULL, NULL },
429         { "objectidentifier", "name> <oid",     3, 3, 0, ARG_MAGIC|CFG_OID,
430                 &config_generic, "( OLcfgGlAt:33 NAME 'olcObjectIdentifier' "
431                         "EQUALITY caseIgnoreMatch "
432                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
433         { "overlay", "overlay", 2, 2, 0, ARG_MAGIC,
434                 &config_overlay, "( OLcfgGlAt:34 NAME 'olcOverlay' "
435                         "SUP olcDatabase SINGLE-VALUE X-ORDERED 'SIBLINGS' )", NULL, NULL },
436         { "password-crypt-salt-format", "salt", 2, 2, 0, ARG_STRING|ARG_MAGIC|CFG_SALT,
437                 &config_generic, "( OLcfgGlAt:35 NAME 'olcPasswordCryptSaltFormat' "
438                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
439         { "password-hash", "hash", 2, 2, 0, ARG_MAGIC,
440                 &config_passwd_hash, "( OLcfgGlAt:36 NAME 'olcPasswordHash' "
441                         "EQUALITY caseIgnoreMatch "
442                         "SYNTAX OMsDirectoryString )", NULL, NULL },
443         { "pidfile", "file", 2, 2, 0, ARG_STRING,
444                 &slapd_pid_file, "( OLcfgGlAt:37 NAME 'olcPidFile' "
445                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
446         { "plugin", NULL, 0, 0, 0,
447 #ifdef LDAP_SLAPI
448                 ARG_MAGIC|CFG_PLUGIN, &config_generic,
449 #else
450                 ARG_IGNORED, NULL,
451 #endif
452                 "( OLcfgGlAt:38 NAME 'olcPlugin' "
453                         "EQUALITY caseIgnoreMatch "
454                         "SYNTAX OMsDirectoryString )", NULL, NULL },
455         { "pluginlog", "filename", 2, 2, 0,
456 #ifdef LDAP_SLAPI
457                 ARG_STRING, &slapi_log_file,
458 #else
459                 ARG_IGNORED, NULL,
460 #endif
461                 "( OLcfgGlAt:39 NAME 'olcPluginLogFile' "
462                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
463         { "readonly", "on|off", 2, 2, 0, ARG_MAY_DB|ARG_ON_OFF|ARG_MAGIC|CFG_RO,
464                 &config_generic, "( OLcfgGlAt:40 NAME 'olcReadOnly' "
465                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
466         { "referral", "url", 2, 2, 0, ARG_MAGIC,
467                 &config_referral, "( OLcfgGlAt:41 NAME 'olcReferral' "
468                         "SUP labeledURI SINGLE-VALUE )", NULL, NULL },
469         { "replica", "host or uri", 2, 0, 0, ARG_DB|ARG_MAGIC,
470                 &config_obsolete, "( OLcfgDbAt:0.7 NAME 'olcReplica' "
471                         "EQUALITY caseIgnoreMatch "
472                         "SUP labeledURI X-ORDERED 'VALUES' )", NULL, NULL },
473         { "replica-argsfile", NULL, 0, 0, 0, ARG_MAY_DB|ARG_MAGIC,
474                 &config_obsolete, "( OLcfgGlAt:43 NAME 'olcReplicaArgsFile' "
475                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
476         { "replica-pidfile", NULL, 0, 0, 0, ARG_MAY_DB|ARG_MAGIC,
477                 &config_obsolete, "( OLcfgGlAt:44 NAME 'olcReplicaPidFile' "
478                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
479         { "replicationInterval", NULL, 0, 0, 0, ARG_MAY_DB|ARG_MAGIC,
480                 &config_obsolete, "( OLcfgGlAt:45 NAME 'olcReplicationInterval' "
481                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
482         { "replogfile", "filename", 2, 2, 0, ARG_MAY_DB|ARG_MAGIC,
483                 &config_obsolete, "( OLcfgGlAt:46 NAME 'olcReplogFile' "
484                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
485         { "require", "features", 2, 0, 7, ARG_MAY_DB|ARG_MAGIC,
486                 &config_requires, "( OLcfgGlAt:47 NAME 'olcRequires' "
487                         "EQUALITY caseIgnoreMatch "
488                         "SYNTAX OMsDirectoryString )", NULL, NULL },
489         { "restrict", "op_list", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
490                 &config_restrict, "( OLcfgGlAt:48 NAME 'olcRestrict' "
491                         "EQUALITY caseIgnoreMatch "
492                         "SYNTAX OMsDirectoryString )", NULL, NULL },
493         { "reverse-lookup", "on|off", 2, 2, 0,
494 #ifdef SLAPD_RLOOKUPS
495                 ARG_ON_OFF, &use_reverse_lookup,
496 #else
497                 ARG_IGNORED, NULL,
498 #endif
499                 "( OLcfgGlAt:49 NAME 'olcReverseLookup' "
500                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
501         { "rootdn", "dn", 2, 2, 0, ARG_DB|ARG_DN|ARG_QUOTE|ARG_MAGIC,
502                 &config_rootdn, "( OLcfgDbAt:0.8 NAME 'olcRootDN' "
503                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
504         { "rootDSE", "file", 2, 2, 0, ARG_MAGIC|CFG_ROOTDSE,
505                 &config_generic, "( OLcfgGlAt:51 NAME 'olcRootDSE' "
506                         "EQUALITY caseIgnoreMatch "
507                         "SYNTAX OMsDirectoryString )", NULL, NULL },
508         { "rootpw", "password", 2, 2, 0, ARG_BERVAL|ARG_DB|ARG_MAGIC,
509                 &config_rootpw, "( OLcfgDbAt:0.9 NAME 'olcRootPW' "
510                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
511         { "sasl-authz-policy", NULL, 2, 2, 0, ARG_MAGIC|CFG_AZPOLICY,
512                 &config_generic, NULL, NULL, NULL },
513         { "sasl-host", "host", 2, 2, 0,
514 #ifdef HAVE_CYRUS_SASL
515                 ARG_STRING|ARG_UNIQUE, &sasl_host,
516 #else
517                 ARG_IGNORED, NULL,
518 #endif
519                 "( OLcfgGlAt:53 NAME 'olcSaslHost' "
520                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
521         { "sasl-realm", "realm", 2, 2, 0,
522 #ifdef HAVE_CYRUS_SASL
523                 ARG_STRING|ARG_UNIQUE, &global_realm,
524 #else
525                 ARG_IGNORED, NULL,
526 #endif
527                 "( OLcfgGlAt:54 NAME 'olcSaslRealm' "
528                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
529         { "sasl-regexp", NULL, 3, 3, 0, ARG_MAGIC|CFG_AZREGEXP,
530                 &config_generic, NULL, NULL, NULL },
531         { "sasl-secprops", "properties", 2, 2, 0,
532 #ifdef HAVE_CYRUS_SASL
533                 ARG_MAGIC|CFG_SASLSECP, &config_generic,
534 #else
535                 ARG_IGNORED, NULL,
536 #endif
537                 "( OLcfgGlAt:56 NAME 'olcSaslSecProps' "
538                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
539         { "saslRegexp", NULL, 3, 3, 0, ARG_MAGIC|CFG_AZREGEXP,
540                 &config_generic, NULL, NULL, NULL },
541         { "schemadn", "dn", 2, 2, 0, ARG_MAY_DB|ARG_DN|ARG_QUOTE|ARG_MAGIC,
542                 &config_schema_dn, "( OLcfgGlAt:58 NAME 'olcSchemaDN' "
543                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
544         { "security", "factors", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
545                 &config_security, "( OLcfgGlAt:59 NAME 'olcSecurity' "
546                         "EQUALITY caseIgnoreMatch "
547                         "SYNTAX OMsDirectoryString )", NULL, NULL },
548         { "serverID", "number> <[URI]", 2, 3, 0, ARG_MAGIC|CFG_SERVERID,
549                 &config_generic, "( OLcfgGlAt:81 NAME 'olcServerID' "
550                         "EQUALITY caseIgnoreMatch "
551                         "SYNTAX OMsDirectoryString )", NULL, NULL },
552         { "sizelimit", "limit", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
553                 &config_sizelimit, "( OLcfgGlAt:60 NAME 'olcSizeLimit' "
554                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
555         { "sockbuf_max_incoming", "max", 2, 2, 0, ARG_BER_LEN_T,
556                 &sockbuf_max_incoming, "( OLcfgGlAt:61 NAME 'olcSockbufMaxIncoming' "
557                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
558         { "sockbuf_max_incoming_auth", "max", 2, 2, 0, ARG_BER_LEN_T,
559                 &sockbuf_max_incoming_auth, "( OLcfgGlAt:62 NAME 'olcSockbufMaxIncomingAuth' "
560                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
561         { "sortvals", "attr", 2, 0, 0, ARG_MAGIC|CFG_SORTVALS,
562                 &config_generic, "( OLcfgGlAt:83 NAME 'olcSortVals' "
563                         "DESC 'Attributes whose values will always be sorted' "
564                         "EQUALITY caseIgnoreMatch "
565                         "SYNTAX OMsDirectoryString )", NULL, NULL },
566         { "subordinate", "[advertise]", 1, 2, 0, ARG_DB|ARG_MAGIC,
567                 &config_subordinate, "( OLcfgDbAt:0.15 NAME 'olcSubordinate' "
568                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
569         { "suffix",     "suffix", 2, 2, 0, ARG_DB|ARG_DN|ARG_QUOTE|ARG_MAGIC,
570                 &config_suffix, "( OLcfgDbAt:0.10 NAME 'olcSuffix' "
571                         "EQUALITY distinguishedNameMatch "
572                         "SYNTAX OMsDN )", NULL, NULL },
573         { "syncrepl", NULL, 0, 0, 0, ARG_DB|ARG_MAGIC,
574                 &syncrepl_config, "( OLcfgDbAt:0.11 NAME 'olcSyncrepl' "
575                         "EQUALITY caseIgnoreMatch "
576                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
577         { "threads", "count", 2, 2, 0,
578 #ifdef NO_THREADS
579                 ARG_IGNORED, NULL,
580 #else
581                 ARG_INT|ARG_MAGIC|CFG_THREADS, &config_generic,
582 #endif
583                 "( OLcfgGlAt:66 NAME 'olcThreads' "
584                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
585         { "timelimit", "limit", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
586                 &config_timelimit, "( OLcfgGlAt:67 NAME 'olcTimeLimit' "
587                         "SYNTAX OMsDirectoryString )", NULL, NULL },
588         { "TLSCACertificateFile", NULL, 0, 0, 0,
589 #ifdef HAVE_TLS
590                 CFG_TLS_CA_FILE|ARG_STRING|ARG_MAGIC, &config_tls_option,
591 #else
592                 ARG_IGNORED, NULL,
593 #endif
594                 "( OLcfgGlAt:68 NAME 'olcTLSCACertificateFile' "
595                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
596         { "TLSCACertificatePath", NULL, 0, 0, 0,
597 #ifdef HAVE_TLS
598                 CFG_TLS_CA_PATH|ARG_STRING|ARG_MAGIC, &config_tls_option,
599 #else
600                 ARG_IGNORED, NULL,
601 #endif
602                 "( OLcfgGlAt:69 NAME 'olcTLSCACertificatePath' "
603                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
604         { "TLSCertificateFile", NULL, 0, 0, 0,
605 #ifdef HAVE_TLS
606                 CFG_TLS_CERT_FILE|ARG_STRING|ARG_MAGIC, &config_tls_option,
607 #else
608                 ARG_IGNORED, NULL,
609 #endif
610                 "( OLcfgGlAt:70 NAME 'olcTLSCertificateFile' "
611                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
612         { "TLSCertificateKeyFile", NULL, 0, 0, 0,
613 #ifdef HAVE_TLS
614                 CFG_TLS_CERT_KEY|ARG_STRING|ARG_MAGIC, &config_tls_option,
615 #else
616                 ARG_IGNORED, NULL,
617 #endif
618                 "( OLcfgGlAt:71 NAME 'olcTLSCertificateKeyFile' "
619                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
620         { "TLSCipherSuite",     NULL, 0, 0, 0,
621 #ifdef HAVE_TLS
622                 CFG_TLS_CIPHER|ARG_STRING|ARG_MAGIC, &config_tls_option,
623 #else
624                 ARG_IGNORED, NULL,
625 #endif
626                 "( OLcfgGlAt:72 NAME 'olcTLSCipherSuite' "
627                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
628         { "TLSCRLCheck", NULL, 0, 0, 0,
629 #if defined(HAVE_TLS) && defined(HAVE_OPENSSL_CRL)
630                 CFG_TLS_CRLCHECK|ARG_STRING|ARG_MAGIC, &config_tls_config,
631 #else
632                 ARG_IGNORED, NULL,
633 #endif
634                 "( OLcfgGlAt:73 NAME 'olcTLSCRLCheck' "
635                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
636         { "TLSCRLFile", NULL, 0, 0, 0,
637 #if defined(HAVE_GNUTLS)
638                 CFG_TLS_CRL_FILE|ARG_STRING|ARG_MAGIC, &config_tls_option,
639 #else
640                 ARG_IGNORED, NULL,
641 #endif
642                 "( OLcfgGlAt:82 NAME 'olcTLSCRLFile' "
643                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
644         { "TLSRandFile", NULL, 0, 0, 0,
645 #ifdef HAVE_TLS
646                 CFG_TLS_RAND|ARG_STRING|ARG_MAGIC, &config_tls_option,
647 #else
648                 ARG_IGNORED, NULL,
649 #endif
650                 "( OLcfgGlAt:74 NAME 'olcTLSRandFile' "
651                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
652         { "TLSVerifyClient", NULL, 0, 0, 0,
653 #ifdef HAVE_TLS
654                 CFG_TLS_VERIFY|ARG_STRING|ARG_MAGIC, &config_tls_config,
655 #else
656                 ARG_IGNORED, NULL,
657 #endif
658                 "( OLcfgGlAt:75 NAME 'olcTLSVerifyClient' "
659                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
660         { "TLSDHParamFile", NULL, 0, 0, 0,
661 #ifdef HAVE_TLS
662                 CFG_TLS_DH_FILE|ARG_STRING|ARG_MAGIC, &config_tls_option,
663 #else
664                 ARG_IGNORED, NULL,
665 #endif
666                 "( OLcfgGlAt:77 NAME 'olcTLSDHParamFile' "
667                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
668         { "tool-threads", "count", 2, 2, 0, ARG_INT|ARG_MAGIC|CFG_TTHREADS,
669                 &config_generic, "( OLcfgGlAt:80 NAME 'olcToolThreads' "
670                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
671         { "ucdata-path", "path", 2, 2, 0, ARG_IGNORED,
672                 NULL, NULL, NULL, NULL },
673         { "updatedn", "dn", 2, 2, 0, ARG_DB|ARG_DN|ARG_QUOTE|ARG_MAGIC,
674                 &config_updatedn, "( OLcfgDbAt:0.12 NAME 'olcUpdateDN' "
675                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
676         { "updateref", "url", 2, 2, 0, ARG_DB|ARG_MAGIC,
677                 &config_updateref, "( OLcfgDbAt:0.13 NAME 'olcUpdateRef' "
678                         "EQUALITY caseIgnoreMatch "
679                         "SUP labeledURI )", NULL, NULL },
680         { NULL, NULL, 0, 0, 0, ARG_IGNORED,
681                 NULL, NULL, NULL, NULL }
682 };
683
684 /* Routines to check if a child can be added to this type */
685 static ConfigLDAPadd cfAddSchema, cfAddInclude, cfAddDatabase,
686         cfAddBackend, cfAddModule, cfAddOverlay;
687
688 /* NOTE: be careful when defining array members
689  * that can be conditionally compiled */
690 #define CFOC_GLOBAL     cf_ocs[1]
691 #define CFOC_SCHEMA     cf_ocs[2]
692 #define CFOC_BACKEND    cf_ocs[3]
693 #define CFOC_DATABASE   cf_ocs[4]
694 #define CFOC_OVERLAY    cf_ocs[5]
695 #define CFOC_INCLUDE    cf_ocs[6]
696 #define CFOC_FRONTEND   cf_ocs[7]
697 #ifdef SLAPD_MODULES
698 #define CFOC_MODULE     cf_ocs[8]
699 #endif /* SLAPD_MODULES */
700
701 static ConfigOCs cf_ocs[] = {
702         { "( OLcfgGlOc:0 "
703                 "NAME 'olcConfig' "
704                 "DESC 'OpenLDAP configuration object' "
705                 "ABSTRACT SUP top )", Cft_Abstract, NULL },
706         { "( OLcfgGlOc:1 "
707                 "NAME 'olcGlobal' "
708                 "DESC 'OpenLDAP Global configuration options' "
709                 "SUP olcConfig STRUCTURAL "
710                 "MAY ( cn $ olcConfigFile $ olcConfigDir $ olcAllows $ olcArgsFile $ "
711                  "olcAttributeOptions $ olcAuthIDRewrite $ "
712                  "olcAuthzPolicy $ olcAuthzRegexp $ olcConcurrency $ "
713                  "olcConnMaxPending $ olcConnMaxPendingAuth $ "
714                  "olcDisallows $ olcGentleHUP $ olcIdleTimeout $ "
715                  "olcIndexSubstrIfMaxLen $ olcIndexSubstrIfMinLen $ "
716                  "olcIndexSubstrAnyLen $ olcIndexSubstrAnyStep $ olcIndexIntLen $ "
717                  "olcLocalSSF $ olcLogLevel $ "
718                  "olcPasswordCryptSaltFormat $ olcPasswordHash $ olcPidFile $ "
719                  "olcPluginLogFile $ olcReadOnly $ olcReferral $ "
720                  "olcReplogFile $ olcRequires $ olcRestrict $ olcReverseLookup $ "
721                  "olcRootDSE $ "
722                  "olcSaslHost $ olcSaslRealm $ olcSaslSecProps $ "
723                  "olcSecurity $ olcServerID $ olcSizeLimit $ "
724                  "olcSockbufMaxIncoming $ olcSockbufMaxIncomingAuth $ "
725                  "olcThreads $ olcTimeLimit $ olcTLSCACertificateFile $ "
726                  "olcTLSCACertificatePath $ olcTLSCertificateFile $ "
727                  "olcTLSCertificateKeyFile $ olcTLSCipherSuite $ olcTLSCRLCheck $ "
728                  "olcTLSRandFile $ olcTLSVerifyClient $ olcTLSDHParamFile $ "
729                  "olcTLSCRLFile $ olcToolThreads $ "
730                  "olcObjectIdentifier $ olcAttributeTypes $ olcObjectClasses $ "
731                  "olcDitContentRules ) )", Cft_Global },
732         { "( OLcfgGlOc:2 "
733                 "NAME 'olcSchemaConfig' "
734                 "DESC 'OpenLDAP schema object' "
735                 "SUP olcConfig STRUCTURAL "
736                 "MAY ( cn $ olcObjectIdentifier $ olcAttributeTypes $ "
737                  "olcObjectClasses $ olcDitContentRules ) )",
738                         Cft_Schema, NULL, cfAddSchema },
739         { "( OLcfgGlOc:3 "
740                 "NAME 'olcBackendConfig' "
741                 "DESC 'OpenLDAP Backend-specific options' "
742                 "SUP olcConfig STRUCTURAL "
743                 "MUST olcBackend )", Cft_Backend, NULL, cfAddBackend },
744         { "( OLcfgGlOc:4 "
745                 "NAME 'olcDatabaseConfig' "
746                 "DESC 'OpenLDAP Database-specific options' "
747                 "SUP olcConfig STRUCTURAL "
748                 "MUST olcDatabase "
749                 "MAY ( olcHidden $ olcSuffix $ olcSubordinate $ olcAccess $ "
750                  "olcLastMod $ olcLimits $ "
751                  "olcMaxDerefDepth $ olcPlugin $ olcReadOnly $ olcReplica $ "
752                  "olcReplicaArgsFile $ olcReplicaPidFile $ olcReplicationInterval $ "
753                  "olcReplogFile $ olcRequires $ olcRestrict $ olcRootDN $ olcRootPW $ "
754                  "olcSchemaDN $ olcSecurity $ olcSizeLimit $ olcSyncrepl $ "
755                  "olcTimeLimit $ olcUpdateDN $ olcUpdateRef $ olcMirrorMode $ "
756                  "olcMonitoring ) )",
757                         Cft_Database, NULL, cfAddDatabase },
758         { "( OLcfgGlOc:5 "
759                 "NAME 'olcOverlayConfig' "
760                 "DESC 'OpenLDAP Overlay-specific options' "
761                 "SUP olcConfig STRUCTURAL "
762                 "MUST olcOverlay )", Cft_Overlay, NULL, cfAddOverlay },
763         { "( OLcfgGlOc:6 "
764                 "NAME 'olcIncludeFile' "
765                 "DESC 'OpenLDAP configuration include file' "
766                 "SUP olcConfig STRUCTURAL "
767                 "MUST olcInclude "
768                 "MAY ( cn $ olcRootDSE ) )",
769                 /* Used to be Cft_Include, that def has been removed */
770                 Cft_Abstract, NULL, cfAddInclude },
771         /* This should be STRUCTURAL like all the other database classes, but
772          * that would mean inheriting all of the olcDatabaseConfig attributes,
773          * which causes them to be merged twice in config_build_entry.
774          */
775         { "( OLcfgGlOc:7 "
776                 "NAME 'olcFrontendConfig' "
777                 "DESC 'OpenLDAP frontend configuration' "
778                 "AUXILIARY "
779                 "MAY ( olcDefaultSearchBase $ olcPasswordHash $ olcSortVals ) )",
780                 Cft_Database, NULL, NULL },
781 #ifdef SLAPD_MODULES
782         { "( OLcfgGlOc:8 "
783                 "NAME 'olcModuleList' "
784                 "DESC 'OpenLDAP dynamic module info' "
785                 "SUP olcConfig STRUCTURAL "
786                 "MAY ( cn $ olcModulePath $ olcModuleLoad ) )",
787                 Cft_Module, NULL, cfAddModule },
788 #endif
789         { NULL, 0, NULL }
790 };
791
792 typedef struct ServerID {
793         struct ServerID *si_next;
794         struct berval si_url;
795         int si_num;
796 } ServerID;
797
798 static ServerID *sid_list;
799
800 typedef struct ADlist {
801         struct ADlist *al_next;
802         AttributeDescription *al_desc;
803 } ADlist;
804
805 static ADlist *sortVals;
806
807 static int
808 config_generic(ConfigArgs *c) {
809         int i;
810
811         if ( c->op == SLAP_CONFIG_EMIT ) {
812                 int rc = 0;
813                 switch(c->type) {
814                 case CFG_CONCUR:
815                         c->value_int = ldap_pvt_thread_get_concurrency();
816                         break;
817                 case CFG_THREADS:
818                         c->value_int = connection_pool_max;
819                         break;
820                 case CFG_TTHREADS:
821                         c->value_int = slap_tool_thread_max;
822                         break;
823                 case CFG_SALT:
824                         if ( passwd_salt )
825                                 c->value_string = ch_strdup( passwd_salt );
826                         else
827                                 rc = 1;
828                         break;
829                 case CFG_LIMITS:
830                         if ( c->be->be_limits ) {
831                                 char buf[4096*3];
832                                 struct berval bv;
833
834                                 for ( i=0; c->be->be_limits[i]; i++ ) {
835                                         bv.bv_len = snprintf( buf, sizeof( buf ), SLAP_X_ORDERED_FMT, i );
836                                         if ( bv.bv_len >= sizeof( buf ) ) {
837                                                 ber_bvarray_free_x( c->rvalue_vals, NULL );
838                                                 c->rvalue_vals = NULL;
839                                                 rc = 1;
840                                                 break;
841                                         }
842                                         bv.bv_val = buf + bv.bv_len;
843                                         limits_unparse( c->be->be_limits[i], &bv,
844                                                         sizeof( buf ) - ( bv.bv_val - buf ) );
845                                         bv.bv_len += bv.bv_val - buf;
846                                         bv.bv_val = buf;
847                                         value_add_one( &c->rvalue_vals, &bv );
848                                 }
849                         }
850                         if ( !c->rvalue_vals ) rc = 1;
851                         break;
852                 case CFG_RO:
853                         c->value_int = (c->be->be_restrictops & SLAP_RESTRICT_OP_WRITES) ==
854                                 SLAP_RESTRICT_OP_WRITES;
855                         break;
856                 case CFG_AZPOLICY:
857                         c->value_string = ch_strdup( slap_sasl_getpolicy());
858                         break;
859                 case CFG_AZREGEXP:
860                         slap_sasl_regexp_unparse( &c->rvalue_vals );
861                         if ( !c->rvalue_vals ) rc = 1;
862                         break;
863 #ifdef HAVE_CYRUS_SASL
864                 case CFG_SASLSECP: {
865                         struct berval bv = BER_BVNULL;
866                         slap_sasl_secprops_unparse( &bv );
867                         if ( !BER_BVISNULL( &bv )) {
868                                 ber_bvarray_add( &c->rvalue_vals, &bv );
869                         } else {
870                                 rc = 1;
871                         }
872                         }
873                         break;
874 #endif
875                 case CFG_DEPTH:
876                         c->value_int = c->be->be_max_deref_depth;
877                         break;
878                 case CFG_HIDDEN:
879                         if ( SLAP_DBHIDDEN( c->be )) {
880                                 c->value_int = 1;
881                         } else {
882                                 rc = 1;
883                         }
884                         break;
885                 case CFG_OID: {
886                         ConfigFile *cf = c->private;
887                         if ( !cf )
888                                 oidm_unparse( &c->rvalue_vals, NULL, NULL, 1 );
889                         else if ( cf->c_om_head )
890                                 oidm_unparse( &c->rvalue_vals, cf->c_om_head,
891                                         cf->c_om_tail, 0 );
892                         if ( !c->rvalue_vals )
893                                 rc = 1;
894                         }
895                         break;
896                 case CFG_ATOPT:
897                         ad_unparse_options( &c->rvalue_vals );
898                         break;
899                 case CFG_OC: {
900                         ConfigFile *cf = c->private;
901                         if ( !cf )
902                                 oc_unparse( &c->rvalue_vals, NULL, NULL, 1 );
903                         else if ( cf->c_oc_head )
904                                 oc_unparse( &c->rvalue_vals, cf->c_oc_head,
905                                         cf->c_oc_tail, 0 );
906                         if ( !c->rvalue_vals )
907                                 rc = 1;
908                         }
909                         break;
910                 case CFG_ATTR: {
911                         ConfigFile *cf = c->private;
912                         if ( !cf )
913                                 at_unparse( &c->rvalue_vals, NULL, NULL, 1 );
914                         else if ( cf->c_at_head )
915                                 at_unparse( &c->rvalue_vals, cf->c_at_head,
916                                         cf->c_at_tail, 0 );
917                         if ( !c->rvalue_vals )
918                                 rc = 1;
919                         }
920                         break;
921                 case CFG_DIT: {
922                         ConfigFile *cf = c->private;
923                         if ( !cf )
924                                 cr_unparse( &c->rvalue_vals, NULL, NULL, 1 );
925                         else if ( cf->c_cr_head )
926                                 cr_unparse( &c->rvalue_vals, cf->c_cr_head,
927                                         cf->c_cr_tail, 0 );
928                         if ( !c->rvalue_vals )
929                                 rc = 1;
930                         }
931                         break;
932                         
933                 case CFG_ACL: {
934                         AccessControl *a;
935                         char *src, *dst, ibuf[11];
936                         struct berval bv, abv;
937                         for (i=0, a=c->be->be_acl; a; i++,a=a->acl_next) {
938                                 abv.bv_len = snprintf( ibuf, sizeof( ibuf ), SLAP_X_ORDERED_FMT, i );
939                                 if ( abv.bv_len >= sizeof( ibuf ) ) {
940                                         ber_bvarray_free_x( c->rvalue_vals, NULL );
941                                         c->rvalue_vals = NULL;
942                                         i = 0;
943                                         break;
944                                 }
945                                 acl_unparse( a, &bv );
946                                 abv.bv_val = ch_malloc( abv.bv_len + bv.bv_len + 1 );
947                                 AC_MEMCPY( abv.bv_val, ibuf, abv.bv_len );
948                                 /* Turn TAB / EOL into plain space */
949                                 for (src=bv.bv_val,dst=abv.bv_val+abv.bv_len; *src; src++) {
950                                         if (isspace((unsigned char)*src)) *dst++ = ' ';
951                                         else *dst++ = *src;
952                                 }
953                                 *dst = '\0';
954                                 if (dst[-1] == ' ') {
955                                         dst--;
956                                         *dst = '\0';
957                                 }
958                                 abv.bv_len = dst - abv.bv_val;
959                                 ber_bvarray_add( &c->rvalue_vals, &abv );
960                         }
961                         rc = (!i);
962                         break;
963                 }
964                 case CFG_ROOTDSE: {
965                         ConfigFile *cf = c->private;
966                         if ( cf->c_dseFiles ) {
967                                 value_add( &c->rvalue_vals, cf->c_dseFiles );
968                         } else {
969                                 rc = 1;
970                         }
971                         }
972                         break;
973                 case CFG_SERVERID:
974                         if ( sid_list ) {
975                                 ServerID *si;
976                                 struct berval bv;
977
978                                 for ( si = sid_list; si; si=si->si_next ) {
979                                         assert( si->si_num >= 0 && si->si_num <= SLAP_SYNC_SID_MAX );
980                                         if ( !BER_BVISEMPTY( &si->si_url )) {
981                                                 bv.bv_len = si->si_url.bv_len + 6;
982                                                 bv.bv_val = ch_malloc( bv.bv_len );
983                                                 sprintf( bv.bv_val, "%d %s", si->si_num,
984                                                         si->si_url.bv_val );
985                                                 ber_bvarray_add( &c->rvalue_vals, &bv );
986                                         } else {
987                                                 char buf[5];
988                                                 bv.bv_val = buf;
989                                                 bv.bv_len = sprintf( buf, "%d", si->si_num );
990                                                 value_add_one( &c->rvalue_vals, &bv );
991                                         }
992                                 }
993                         } else {
994                                 rc = 1;
995                         }
996                         break;
997                 case CFG_LOGFILE:
998                         if ( logfileName )
999                                 c->value_string = ch_strdup( logfileName );
1000                         else
1001                                 rc = 1;
1002                         break;
1003                 case CFG_LASTMOD:
1004                         c->value_int = (SLAP_NOLASTMOD(c->be) == 0);
1005                         break;
1006                 case CFG_MIRRORMODE:
1007                         if ( SLAP_SHADOW(c->be))
1008                                 c->value_int = (SLAP_SINGLE_SHADOW(c->be) == 0);
1009                         else
1010                                 rc = 1;
1011                         break;
1012                 case CFG_MONITORING:
1013                         c->value_int = (SLAP_DBMONITORING(c->be) != 0);
1014                         break;
1015                 case CFG_SSTR_IF_MAX:
1016                         c->value_int = index_substr_if_maxlen;
1017                         break;
1018                 case CFG_SSTR_IF_MIN:
1019                         c->value_int = index_substr_if_minlen;
1020                         break;
1021                 case CFG_IX_INTLEN:
1022                         c->value_int = index_intlen;
1023                         break;
1024                 case CFG_SORTVALS: {
1025                         ADlist *sv;
1026                         rc = 1;
1027                         for ( sv = sortVals; sv; sv = sv->al_next ) {
1028                                 value_add_one( &c->rvalue_vals, &sv->al_desc->ad_cname );
1029                                 rc = 0;
1030                         }
1031                         } break;
1032 #ifdef SLAPD_MODULES
1033                 case CFG_MODLOAD: {
1034                         ModPaths *mp = c->private;
1035                         if (mp->mp_loads) {
1036                                 int i;
1037                                 for (i=0; !BER_BVISNULL(&mp->mp_loads[i]); i++) {
1038                                         struct berval bv;
1039                                         bv.bv_val = c->log;
1040                                         bv.bv_len = snprintf( bv.bv_val, sizeof( c->log ),
1041                                                 SLAP_X_ORDERED_FMT "%s", i,
1042                                                 mp->mp_loads[i].bv_val );
1043                                         if ( bv.bv_len >= sizeof( c->log ) ) {
1044                                                 ber_bvarray_free_x( c->rvalue_vals, NULL );
1045                                                 c->rvalue_vals = NULL;
1046                                                 break;
1047                                         }
1048                                         value_add_one( &c->rvalue_vals, &bv );
1049                                 }
1050                         }
1051
1052                         rc = c->rvalue_vals ? 0 : 1;
1053                         }
1054                         break;
1055                 case CFG_MODPATH: {
1056                         ModPaths *mp = c->private;
1057                         if ( !BER_BVISNULL( &mp->mp_path ))
1058                                 value_add_one( &c->rvalue_vals, &mp->mp_path );
1059
1060                         rc = c->rvalue_vals ? 0 : 1;
1061                         }
1062                         break;
1063 #endif
1064 #ifdef LDAP_SLAPI
1065                 case CFG_PLUGIN:
1066                         slapi_int_plugin_unparse( c->be, &c->rvalue_vals );
1067                         if ( !c->rvalue_vals ) rc = 1;
1068                         break;
1069 #endif
1070 #ifdef SLAP_AUTH_REWRITE
1071                 case CFG_REWRITE:
1072                         if ( authz_rewrites ) {
1073                                 struct berval bv, idx;
1074                                 char ibuf[32];
1075                                 int i;
1076
1077                                 idx.bv_val = ibuf;
1078                                 for ( i=0; !BER_BVISNULL( &authz_rewrites[i] ); i++ ) {
1079                                         idx.bv_len = snprintf( idx.bv_val, sizeof( ibuf ), SLAP_X_ORDERED_FMT, i );
1080                                         if ( idx.bv_len >= sizeof( ibuf ) ) {
1081                                                 ber_bvarray_free_x( c->rvalue_vals, NULL );
1082                                                 c->rvalue_vals = NULL;
1083                                                 break;
1084                                         }
1085                                         bv.bv_len = idx.bv_len + authz_rewrites[i].bv_len;
1086                                         bv.bv_val = ch_malloc( bv.bv_len + 1 );
1087                                         AC_MEMCPY( bv.bv_val, idx.bv_val, idx.bv_len );
1088                                         AC_MEMCPY( &bv.bv_val[ idx.bv_len ],
1089                                                 authz_rewrites[i].bv_val,
1090                                                 authz_rewrites[i].bv_len + 1 );
1091                                         ber_bvarray_add( &c->rvalue_vals, &bv );
1092                                 }
1093                         }
1094                         if ( !c->rvalue_vals ) rc = 1;
1095                         break;
1096 #endif
1097                 default:
1098                         rc = 1;
1099                 }
1100                 return rc;
1101         } else if ( c->op == LDAP_MOD_DELETE ) {
1102                 int rc = 0;
1103                 switch(c->type) {
1104                 /* single-valued attrs, no-ops */
1105                 case CFG_CONCUR:
1106                 case CFG_THREADS:
1107                 case CFG_TTHREADS:
1108                 case CFG_RO:
1109                 case CFG_AZPOLICY:
1110                 case CFG_DEPTH:
1111                 case CFG_LASTMOD:
1112                 case CFG_MIRRORMODE:
1113                 case CFG_MONITORING:
1114                 case CFG_SASLSECP:
1115                 case CFG_SSTR_IF_MAX:
1116                 case CFG_SSTR_IF_MIN:
1117                         break;
1118
1119                 /* no-ops, requires slapd restart */
1120                 case CFG_PLUGIN:
1121                 case CFG_MODLOAD:
1122                 case CFG_AZREGEXP:
1123                 case CFG_REWRITE:
1124                         snprintf(c->log, sizeof( c->log ), "change requires slapd restart");
1125                         break;
1126
1127                 case CFG_SALT:
1128                         ch_free( passwd_salt );
1129                         passwd_salt = NULL;
1130                         break;
1131
1132                 case CFG_LOGFILE:
1133                         ch_free( logfileName );
1134                         logfileName = NULL;
1135                         if ( logfile ) {
1136                                 fclose( logfile );
1137                                 logfile = NULL;
1138                         }
1139                         break;
1140
1141                 case CFG_SERVERID: {
1142                         ServerID *si, **sip;
1143
1144                         for ( i=0, si = sid_list, sip = &sid_list;
1145                                 si; si = *sip, i++ ) {
1146                                 if ( c->valx == -1 || i == c->valx ) {
1147                                         *sip = si->si_next;
1148                                         ch_free( si );
1149                                         if ( c->valx >= 0 )
1150                                                 break;
1151                                 } else {
1152                                         sip = &si->si_next;
1153                                 }
1154                         }
1155                         }
1156                         break;
1157                 case CFG_HIDDEN:
1158                         c->be->be_flags &= ~SLAP_DBFLAG_HIDDEN;
1159                         break;
1160
1161                 case CFG_IX_INTLEN:
1162                         index_intlen = SLAP_INDEX_INTLEN_DEFAULT;
1163                         index_intlen_strlen = SLAP_INDEX_INTLEN_STRLEN(
1164                                 SLAP_INDEX_INTLEN_DEFAULT );
1165                         break;
1166
1167                 case CFG_ACL:
1168                         if ( c->valx < 0 ) {
1169                                 AccessControl *end;
1170                                 if ( c->be == frontendDB )
1171                                         end = NULL;
1172                                 else
1173                                         end = frontendDB->be_acl;
1174                                 acl_destroy( c->be->be_acl, end );
1175                                 c->be->be_acl = end;
1176
1177                         } else {
1178                                 AccessControl **prev, *a;
1179                                 int i;
1180                                 for (i=0, prev = &c->be->be_acl; i < c->valx;
1181                                         i++ ) {
1182                                         a = *prev;
1183                                         prev = &a->acl_next;
1184                                 }
1185                                 a = *prev;
1186                                 *prev = a->acl_next;
1187                                 acl_free( a );
1188                         }
1189                         break;
1190
1191                 case CFG_OC: {
1192                         CfEntryInfo *ce;
1193                         /* Can be NULL when undoing a failed add */
1194                         if ( c->ca_entry ) {
1195                                 ce = c->ca_entry->e_private;
1196                                 /* can't modify the hardcoded schema */
1197                                 if ( ce->ce_parent->ce_type == Cft_Global )
1198                                         return 1;
1199                                 }
1200                         }
1201                         cfn = c->private;
1202                         if ( c->valx < 0 ) {
1203                                 ObjectClass *oc;
1204
1205                                 for( oc = cfn->c_oc_head; oc; oc_next( &oc )) {
1206                                         oc_delete( oc );
1207                                         if ( oc  == cfn->c_oc_tail )
1208                                                 break;
1209                                 }
1210                                 cfn->c_oc_head = cfn->c_oc_tail = NULL;
1211                         } else {
1212                                 ObjectClass *oc, *prev = NULL;
1213
1214                                 for ( i=0, oc=cfn->c_oc_head; i<c->valx; i++) {
1215                                         prev = oc;
1216                                         oc_next( &oc );
1217                                 }
1218                                 oc_delete( oc );
1219                                 if ( cfn->c_oc_tail == oc ) {
1220                                         cfn->c_oc_tail = prev;
1221                                 }
1222                                 if ( cfn->c_oc_head == oc ) {
1223                                         oc_next( &oc );
1224                                         cfn->c_oc_head = oc;
1225                                 }
1226                         }
1227                         break;
1228
1229                 case CFG_ATTR: {
1230                         CfEntryInfo *ce;
1231                         /* Can be NULL when undoing a failed add */
1232                         if ( c->ca_entry ) {
1233                                 ce = c->ca_entry->e_private;
1234                                 /* can't modify the hardcoded schema */
1235                                 if ( ce->ce_parent->ce_type == Cft_Global )
1236                                         return 1;
1237                                 }
1238                         }
1239                         cfn = c->private;
1240                         if ( c->valx < 0 ) {
1241                                 AttributeType *at;
1242
1243                                 for( at = cfn->c_at_head; at; at_next( &at )) {
1244                                         at_delete( at );
1245                                         if ( at  == cfn->c_at_tail )
1246                                                 break;
1247                                 }
1248                                 cfn->c_at_head = cfn->c_at_tail = NULL;
1249                         } else {
1250                                 AttributeType *at, *prev = NULL;
1251
1252                                 for ( i=0, at=cfn->c_at_head; i<c->valx; i++) {
1253                                         prev = at;
1254                                         at_next( &at );
1255                                 }
1256                                 at_delete( at );
1257                                 if ( cfn->c_at_tail == at ) {
1258                                         cfn->c_at_tail = prev;
1259                                 }
1260                                 if ( cfn->c_at_head == at ) {
1261                                         at_next( &at );
1262                                         cfn->c_at_head = at;
1263                                 }
1264                         }
1265                         break;
1266                 case CFG_SORTVALS:
1267                         if ( c->valx < 0 ) {
1268                                 ADlist *sv;
1269                                 for ( sv = sortVals; sv; sv = sortVals ) {
1270                                         sortVals = sv->al_next;
1271                                         sv->al_desc->ad_type->sat_flags &= ~SLAP_AT_SORTED_VAL;
1272                                         ch_free( sv );
1273                                 }
1274                         } else {
1275                                 ADlist *sv, **prev;
1276                                 int i = 0;
1277
1278                                 for ( prev = &sortVals, sv = sortVals; i < c->valx; i++ ) {
1279                                         prev = &sv->al_next;
1280                                         sv = sv->al_next;
1281                                 }
1282                                 sv->al_desc->ad_type->sat_flags &= ~SLAP_AT_SORTED_VAL;
1283                                 *prev = sv->al_next;
1284                                 ch_free( sv );
1285                         }
1286                         break;
1287
1288                 case CFG_LIMITS:
1289                         /* FIXME: there is no limits_free function */
1290                 case CFG_ATOPT:
1291                         /* FIXME: there is no ad_option_free function */
1292                 case CFG_ROOTDSE:
1293                         /* FIXME: there is no way to remove attributes added by
1294                                 a DSE file */
1295                 case CFG_OID:
1296                 case CFG_DIT:
1297                 case CFG_MODPATH:
1298                 default:
1299                         rc = 1;
1300                         break;
1301                 }
1302                 return rc;
1303         }
1304
1305         switch(c->type) {
1306                 case CFG_BACKEND:
1307                         if(!(c->bi = backend_info(c->argv[1]))) {
1308                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> failed init", c->argv[0] );
1309                                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)!\n",
1310                                         c->log, c->cr_msg, c->argv[1] );
1311                                 return(1);
1312                         }
1313                         break;
1314
1315                 case CFG_DATABASE:
1316                         c->bi = NULL;
1317                         /* NOTE: config is always the first backend!
1318                          */
1319                         if ( !strcasecmp( c->argv[1], "config" )) {
1320                                 c->be = LDAP_STAILQ_FIRST(&backendDB);
1321                         } else if ( !strcasecmp( c->argv[1], "frontend" )) {
1322                                 c->be = frontendDB;
1323                         } else {
1324                                 c->be = backend_db_init(c->argv[1], NULL, c->valx, &c->reply);
1325                                 if ( !c->be ) {
1326                                         if ( c->cr_msg[0] == 0 )
1327                                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> failed init", c->argv[0] );
1328                                         Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n", c->log, c->cr_msg, c->argv[1] );
1329                                         return(1);
1330                                 }
1331                         }
1332                         break;
1333
1334                 case CFG_CONCUR:
1335                         ldap_pvt_thread_set_concurrency(c->value_int);
1336                         break;
1337
1338                 case CFG_THREADS:
1339                         if ( c->value_int < 2 ) {
1340                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1341                                         "threads=%d smaller than minimum value 2",
1342                                         c->value_int );
1343                                 Debug(LDAP_DEBUG_ANY, "%s: %s.\n",
1344                                         c->log, c->cr_msg, 0 );
1345                                 return 1;
1346
1347                         } else if ( c->value_int > 2 * SLAP_MAX_WORKER_THREADS ) {
1348                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1349                                         "warning, threads=%d larger than twice the default (2*%d=%d); YMMV",
1350                                         c->value_int, SLAP_MAX_WORKER_THREADS, 2 * SLAP_MAX_WORKER_THREADS );
1351                                 Debug(LDAP_DEBUG_ANY, "%s: %s.\n",
1352                                         c->log, c->cr_msg, 0 );
1353                         }
1354                         if ( slapMode & SLAP_SERVER_MODE )
1355                                 ldap_pvt_thread_pool_maxthreads(&connection_pool, c->value_int);
1356                         connection_pool_max = c->value_int;     /* save for reference */
1357                         break;
1358
1359                 case CFG_TTHREADS:
1360                         if ( slapMode & SLAP_TOOL_MODE )
1361                                 ldap_pvt_thread_pool_maxthreads(&connection_pool, c->value_int);
1362                         slap_tool_thread_max = c->value_int;    /* save for reference */
1363                         break;
1364
1365                 case CFG_SALT:
1366                         if ( passwd_salt ) ch_free( passwd_salt );
1367                         passwd_salt = c->value_string;
1368                         lutil_salt_format(passwd_salt);
1369                         break;
1370
1371                 case CFG_LIMITS:
1372                         if(limits_parse(c->be, c->fname, c->lineno, c->argc, c->argv))
1373                                 return(1);
1374                         break;
1375
1376                 case CFG_RO:
1377                         if(c->value_int)
1378                                 c->be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
1379                         else
1380                                 c->be->be_restrictops &= ~SLAP_RESTRICT_OP_WRITES;
1381                         break;
1382
1383                 case CFG_AZPOLICY:
1384                         ch_free(c->value_string);
1385                         if (slap_sasl_setpolicy( c->argv[1] )) {
1386                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to parse value", c->argv[0] );
1387                                 Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
1388                                         c->log, c->cr_msg, c->argv[1] );
1389                                 return(1);
1390                         }
1391                         break;
1392                 
1393                 case CFG_AZREGEXP:
1394                         if (slap_sasl_regexp_config( c->argv[1], c->argv[2] ))
1395                                 return(1);
1396                         break;
1397                                 
1398 #ifdef HAVE_CYRUS_SASL
1399                 case CFG_SASLSECP:
1400                         {
1401                         char *txt = slap_sasl_secprops( c->argv[1] );
1402                         if ( txt ) {
1403                                 snprintf( c->cr_msg, sizeof(c->cr_msg), "<%s> %s",
1404                                         c->argv[0], txt );
1405                                 Debug(LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
1406                                 return(1);
1407                         }
1408                         break;
1409                         }
1410 #endif
1411
1412                 case CFG_DEPTH:
1413                         c->be->be_max_deref_depth = c->value_int;
1414                         break;
1415
1416                 case CFG_OID: {
1417                         OidMacro *om;
1418
1419                         if ( c->op == LDAP_MOD_ADD && c->private && cfn != c->private )
1420                                 cfn = c->private;
1421                         if(parse_oidm(c, 1, &om))
1422                                 return(1);
1423                         if (!cfn->c_om_head) cfn->c_om_head = om;
1424                         cfn->c_om_tail = om;
1425                         }
1426                         break;
1427
1428                 case CFG_OC: {
1429                         ObjectClass *oc, *prev;
1430
1431                         if ( c->op == LDAP_MOD_ADD && c->private && cfn != c->private )
1432                                 cfn = c->private;
1433                         if ( c->valx < 0 ) {
1434                                 prev = cfn->c_oc_tail;
1435                         } else {
1436                                 prev = NULL;
1437                                 /* If adding anything after the first, prev is easy */
1438                                 if ( c->valx ) {
1439                                         int i;
1440                                         for (i=0, oc = cfn->c_oc_head; i<c->valx; i++) {
1441                                                 prev = oc;
1442                                                 oc_next( &oc );
1443                                         }
1444                                 } else
1445                                 /* If adding the first, and head exists, find its prev */
1446                                         if (cfn->c_oc_head) {
1447                                         for ( oc_start( &oc ); oc != cfn->c_oc_head; ) {
1448                                                 prev = oc;
1449                                                 oc_next( &oc );
1450                                         }
1451                                 }
1452                                 /* else prev is NULL, append to end of global list */
1453                         }
1454                         if(parse_oc(c, &oc, prev)) return(1);
1455                         if (!cfn->c_oc_head) cfn->c_oc_head = oc;
1456                         if (cfn->c_oc_tail == prev) cfn->c_oc_tail = oc;
1457                         }
1458                         break;
1459
1460                 case CFG_ATTR: {
1461                         AttributeType *at, *prev;
1462
1463                         if ( c->op == LDAP_MOD_ADD && c->private && cfn != c->private )
1464                                 cfn = c->private;
1465                         if ( c->valx < 0 ) {
1466                                 prev = cfn->c_at_tail;
1467                         } else {
1468                                 prev = NULL;
1469                                 /* If adding anything after the first, prev is easy */
1470                                 if ( c->valx ) {
1471                                         int i;
1472                                         for (i=0, at = cfn->c_at_head; i<c->valx; i++) {
1473                                                 prev = at;
1474                                                 at_next( &at );
1475                                         }
1476                                 } else
1477                                 /* If adding the first, and head exists, find its prev */
1478                                         if (cfn->c_at_head) {
1479                                         for ( at_start( &at ); at != cfn->c_at_head; ) {
1480                                                 prev = at;
1481                                                 at_next( &at );
1482                                         }
1483                                 }
1484                                 /* else prev is NULL, append to end of global list */
1485                         }
1486                         if(parse_at(c, &at, prev)) return(1);
1487                         if (!cfn->c_at_head) cfn->c_at_head = at;
1488                         if (cfn->c_at_tail == prev) cfn->c_at_tail = at;
1489                         }
1490                         break;
1491
1492                 case CFG_DIT: {
1493                         ContentRule *cr;
1494
1495                         if ( c->op == LDAP_MOD_ADD && c->private && cfn != c->private )
1496                                 cfn = c->private;
1497                         if(parse_cr(c, &cr)) return(1);
1498                         if (!cfn->c_cr_head) cfn->c_cr_head = cr;
1499                         cfn->c_cr_tail = cr;
1500                         }
1501                         break;
1502
1503                 case CFG_ATOPT:
1504                         ad_define_option(NULL, NULL, 0);
1505                         for(i = 1; i < c->argc; i++)
1506                                 if(ad_define_option(c->argv[i], c->fname, c->lineno))
1507                                         return(1);
1508                         break;
1509
1510                 case CFG_IX_INTLEN:
1511                         if ( c->value_int < SLAP_INDEX_INTLEN_DEFAULT )
1512                                 c->value_int = SLAP_INDEX_INTLEN_DEFAULT;
1513                         else if ( c->value_int > 255 )
1514                                 c->value_int = 255;
1515                         index_intlen = c->value_int;
1516                         index_intlen_strlen = SLAP_INDEX_INTLEN_STRLEN(
1517                                 index_intlen );
1518                         break;
1519                         
1520                 case CFG_SORTVALS: {
1521                         ADlist *svnew = NULL, *svtail, *sv;
1522
1523                         for ( i = 1; i < c->argc; i++ ) {
1524                                 AttributeDescription *ad = NULL;
1525                                 const char *text;
1526                                 int rc;
1527
1528                                 rc = slap_str2ad( c->argv[i], &ad, &text );
1529                                 if ( rc ) {
1530                                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown attribute type #%d",
1531                                                 c->argv[0], i );
1532 sortval_reject:
1533                                         Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1534                                                 c->log, c->cr_msg, c->argv[i] );
1535                                         for ( sv = svnew; sv; sv = svnew ) {
1536                                                 svnew = sv->al_next;
1537                                                 ch_free( sv );
1538                                         }
1539                                         return 1;
1540                                 }
1541                                 if (( ad->ad_type->sat_flags & SLAP_AT_ORDERED ) ||
1542                                         ad->ad_type->sat_single_value ) {
1543                                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> inappropriate attribute type #%d",
1544                                                 c->argv[0], i );
1545                                         goto sortval_reject;
1546                                 }
1547                                 sv = ch_malloc( sizeof( ADlist ));
1548                                 sv->al_desc = ad;
1549                                 if ( !svnew ) {
1550                                         svnew = sv;
1551                                 } else {
1552                                         svtail->al_next = sv;
1553                                 }
1554                                 svtail = sv;
1555                         }
1556                         sv->al_next = NULL;
1557                         for ( sv = svnew; sv; sv = sv->al_next )
1558                                 sv->al_desc->ad_type->sat_flags |= SLAP_AT_SORTED_VAL;
1559                         for ( sv = sortVals; sv && sv->al_next; sv = sv->al_next );
1560                         if ( sv )
1561                                 sv->al_next = svnew;
1562                         else
1563                                 sortVals = svnew;
1564                         }
1565                         break;
1566
1567                 case CFG_ACL:
1568                         /* Don't append to the global ACL if we're on a specific DB */
1569                         i = c->valx;
1570                         if ( c->be != frontendDB && frontendDB->be_acl && c->valx == -1 ) {
1571                                 AccessControl *a;
1572                                 i = 0;
1573                                 for ( a=c->be->be_acl; a && a != frontendDB->be_acl;
1574                                         a = a->acl_next )
1575                                         i++;
1576                         }
1577                         if ( parse_acl(c->be, c->fname, c->lineno, c->argc, c->argv, i ) ) {
1578                                 return 1;
1579                         }
1580                         break;
1581
1582                 case CFG_ROOTDSE:
1583                         if(root_dse_read_file(c->argv[1])) {
1584                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> could not read file", c->argv[0] );
1585                                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1586                                         c->log, c->cr_msg, c->argv[1] );
1587                                 return(1);
1588                         }
1589                         {
1590                                 struct berval bv;
1591                                 ber_str2bv( c->argv[1], 0, 1, &bv );
1592                                 if ( c->op == LDAP_MOD_ADD && c->private && cfn != c->private )
1593                                         cfn = c->private;
1594                                 ber_bvarray_add( &cfn->c_dseFiles, &bv );
1595                         }
1596                         break;
1597
1598                 case CFG_SERVERID:
1599                         {
1600                                 ServerID *si, **sip;
1601                                 LDAPURLDesc *lud;
1602                                 int num;
1603                                 if ( lutil_atoi( &num, c->argv[1] ) ||
1604                                         num < 0 || num > SLAP_SYNC_SID_MAX )
1605                                 {
1606                                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
1607                                                 "<%s> illegal server ID", c->argv[0] );
1608                                         Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1609                                                 c->log, c->cr_msg, c->argv[1] );
1610                                         return 1;
1611                                 }
1612                                 /* only one value allowed if no URL is given */
1613                                 if ( c->argc > 2 ) {
1614                                         int len;
1615
1616                                         if ( sid_list && BER_BVISEMPTY( &sid_list->si_url )) {
1617                                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1618                                                         "<%s> only one server ID allowed now", c->argv[0] );
1619                                                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1620                                                         c->log, c->cr_msg, c->argv[1] );
1621                                                 return 1;
1622                                         }
1623
1624                                         if ( ldap_url_parse( c->argv[2], &lud )) {
1625                                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1626                                                         "<%s> invalid URL", c->argv[0] );
1627                                                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1628                                                         c->log, c->cr_msg, c->argv[2] );
1629                                                 return 1;
1630                                         }
1631                                         len = strlen( c->argv[2] );
1632                                         si = ch_malloc( sizeof(ServerID) + len + 1 );
1633                                         si->si_url.bv_val = (char *)(si+1);
1634                                         si->si_url.bv_len = len;
1635                                         strcpy( si->si_url.bv_val, c->argv[2] );
1636                                 } else {
1637                                         if ( sid_list ) {
1638                                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1639                                                         "<%s> unqualified server ID not allowed now", c->argv[0] );
1640                                                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1641                                                         c->log, c->cr_msg, c->argv[1] );
1642                                                 return 1;
1643                                         }
1644                                         si = ch_malloc( sizeof(ServerID) );
1645                                         BER_BVZERO( &si->si_url );
1646                                         slap_serverID = num;
1647                                         Debug( LDAP_DEBUG_CONFIG,
1648                                                 "%s: SID=%d\n",
1649                                                 c->log, slap_serverID, 0 );
1650                                 }
1651                                 si->si_next = NULL;
1652                                 si->si_num = num;
1653                                 for ( sip = &sid_list; *sip; sip = &(*sip)->si_next );
1654                                 *sip = si;
1655
1656                                 if (( slapMode & SLAP_SERVER_MODE ) && c->argc > 2 ) {
1657                                         /* If hostname is empty, or is localhost, or matches
1658                                          * our hostname, this serverID refers to this host.
1659                                          * Compare it against listeners and ports.
1660                                          */
1661                                         if ( !lud->lud_host || !lud->lud_host[0] ||
1662                                                 !strncasecmp("localhost", lud->lud_host,
1663                                                         STRLENOF("localhost")) ||
1664                                                 !strcasecmp( global_host, lud->lud_host )) {
1665                                                 Listener **l = slapd_get_listeners();
1666                                                 int i;
1667
1668                                                 for ( i=0; l[i]; i++ ) {
1669                                                         LDAPURLDesc *lu2;
1670                                                         int isMe = 0;
1671                                                         ldap_url_parse( l[i]->sl_url.bv_val, &lu2 );
1672                                                         do {
1673                                                                 if ( strcasecmp( lud->lud_scheme,
1674                                                                         lu2->lud_scheme ))
1675                                                                         break;
1676                                                                 if ( lud->lud_port != lu2->lud_port )
1677                                                                         break;
1678                                                                 /* Listener on ANY address */
1679                                                                 if ( !lu2->lud_host || !lu2->lud_host[0] ) {
1680                                                                         isMe = 1;
1681                                                                         break;
1682                                                                 }
1683                                                                 /* URL on ANY address */
1684                                                                 if ( !lud->lud_host || !lud->lud_host[0] ) {
1685                                                                         isMe = 1;
1686                                                                         break;
1687                                                                 }
1688                                                                 /* Listener has specific host, must
1689                                                                  * match it
1690                                                                  */
1691                                                                 if ( !strcasecmp( lud->lud_host,
1692                                                                         lu2->lud_host )) {
1693                                                                         isMe = 1;
1694                                                                         break;
1695                                                                 }
1696                                                         } while(0);
1697                                                         ldap_free_urldesc( lu2 );
1698                                                         if ( isMe ) {
1699                                                                 slap_serverID = si->si_num;
1700                                                                 Debug( LDAP_DEBUG_CONFIG,
1701                                                                         "%s: SID=%d (listener=%s)\n",
1702                                                                         c->log, slap_serverID,
1703                                                                         l[i]->sl_url.bv_val );
1704                                                                 break;
1705                                                         }
1706                                                 }
1707                                         }
1708                                 }
1709                                 if ( c->argc > 2 )
1710                                         ldap_free_urldesc( lud );
1711                         }
1712                         break;
1713                 case CFG_LOGFILE: {
1714                                 if ( logfileName ) ch_free( logfileName );
1715                                 logfileName = c->value_string;
1716                                 logfile = fopen(logfileName, "w");
1717                                 if(logfile) lutil_debug_file(logfile);
1718                         } break;
1719
1720                 case CFG_LASTMOD:
1721                         if(SLAP_NOLASTMODCMD(c->be)) {
1722                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> not available for %s database",
1723                                         c->argv[0], c->be->bd_info->bi_type );
1724                                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
1725                                         c->log, c->cr_msg, 0 );
1726                                 return(1);
1727                         }
1728                         if(c->value_int)
1729                                 SLAP_DBFLAGS(c->be) &= ~SLAP_DBFLAG_NOLASTMOD;
1730                         else
1731                                 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_NOLASTMOD;
1732                         break;
1733
1734                 case CFG_MIRRORMODE:
1735                         if(!SLAP_SHADOW(c->be)) {
1736                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> database is not a shadow",
1737                                         c->argv[0] );
1738                                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
1739                                         c->log, c->cr_msg, 0 );
1740                                 return(1);
1741                         }
1742                         if(c->value_int)
1743                                 SLAP_DBFLAGS(c->be) &= ~SLAP_DBFLAG_SINGLE_SHADOW;
1744                         else
1745                                 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_SINGLE_SHADOW;
1746                         break;
1747
1748                 case CFG_MONITORING:
1749                         if(c->value_int)
1750                                 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_MONITORING;
1751                         else
1752                                 SLAP_DBFLAGS(c->be) &= ~SLAP_DBFLAG_MONITORING;
1753                         break;
1754
1755                 case CFG_HIDDEN:
1756                         if (c->value_int)
1757                                 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_HIDDEN;
1758                         else
1759                                 SLAP_DBFLAGS(c->be) &= ~SLAP_DBFLAG_HIDDEN;
1760                         break;
1761
1762                 case CFG_SSTR_IF_MAX:
1763                         if (c->value_int < index_substr_if_minlen) {
1764                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid value", c->argv[0] );
1765                                 Debug(LDAP_DEBUG_ANY, "%s: %s (%d)\n",
1766                                         c->log, c->cr_msg, c->value_int );
1767                                 return(1);
1768                         }
1769                         index_substr_if_maxlen = c->value_int;
1770                         break;
1771
1772                 case CFG_SSTR_IF_MIN:
1773                         if (c->value_int > index_substr_if_maxlen) {
1774                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid value", c->argv[0] );
1775                                 Debug(LDAP_DEBUG_ANY, "%s: %s (%d)\n",
1776                                         c->log, c->cr_msg, c->value_int );
1777                                 return(1);
1778                         }
1779                         index_substr_if_minlen = c->value_int;
1780                         break;
1781
1782 #ifdef SLAPD_MODULES
1783                 case CFG_MODLOAD:
1784                         /* If we're just adding a module on an existing modpath,
1785                          * make sure we've selected the current path.
1786                          */
1787                         if ( c->op == LDAP_MOD_ADD && c->private && modcur != c->private ) {
1788                                 modcur = c->private;
1789                                 /* This should never fail */
1790                                 if ( module_path( modcur->mp_path.bv_val )) {
1791                                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> module path no longer valid",
1792                                                 c->argv[0] );
1793                                         Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
1794                                                 c->log, c->cr_msg, modcur->mp_path.bv_val );
1795                                         return(1);
1796                                 }
1797                         }
1798                         if(module_load(c->argv[1], c->argc - 2, (c->argc > 2) ? c->argv + 2 : NULL))
1799                                 return(1);
1800                         /* Record this load on the current path */
1801                         {
1802                                 struct berval bv;
1803                                 char *ptr;
1804                                 if ( c->op == SLAP_CONFIG_ADD ) {
1805                                         ptr = c->line + STRLENOF("moduleload");
1806                                         while (!isspace((unsigned char) *ptr)) ptr++;
1807                                         while (isspace((unsigned char) *ptr)) ptr++;
1808                                 } else {
1809                                         ptr = c->line;
1810                                 }
1811                                 ber_str2bv(ptr, 0, 1, &bv);
1812                                 ber_bvarray_add( &modcur->mp_loads, &bv );
1813                         }
1814                         /* Check for any new hardcoded schema */
1815                         if ( c->op == LDAP_MOD_ADD && CONFIG_ONLINE_ADD( c )) {
1816                                 config_check_schema( NULL, &cfBackInfo );
1817                         }
1818                         break;
1819
1820                 case CFG_MODPATH:
1821                         if(module_path(c->argv[1])) return(1);
1822                         /* Record which path was used with each module */
1823                         {
1824                                 ModPaths *mp;
1825
1826                                 if (!modpaths.mp_loads) {
1827                                         mp = &modpaths;
1828                                 } else {
1829                                         mp = ch_malloc( sizeof( ModPaths ));
1830                                         modlast->mp_next = mp;
1831                                 }
1832                                 ber_str2bv(c->argv[1], 0, 1, &mp->mp_path);
1833                                 mp->mp_next = NULL;
1834                                 mp->mp_loads = NULL;
1835                                 modlast = mp;
1836                                 c->private = mp;
1837                                 modcur = mp;
1838                         }
1839                         
1840                         break;
1841 #endif
1842
1843 #ifdef LDAP_SLAPI
1844                 case CFG_PLUGIN:
1845                         if(slapi_int_read_config(c->be, c->fname, c->lineno, c->argc, c->argv) != LDAP_SUCCESS)
1846                                 return(1);
1847                         slapi_plugins_used++;
1848                         break;
1849 #endif
1850
1851 #ifdef SLAP_AUTH_REWRITE
1852                 case CFG_REWRITE: {
1853                         struct berval bv;
1854                         char *line;
1855                         
1856                         if(slap_sasl_rewrite_config(c->fname, c->lineno, c->argc, c->argv))
1857                                 return(1);
1858
1859                         if ( c->argc > 1 ) {
1860                                 char    *s;
1861
1862                                 /* quote all args but the first */
1863                                 line = ldap_charray2str( c->argv, "\" \"" );
1864                                 ber_str2bv( line, 0, 0, &bv );
1865                                 s = ber_bvchr( &bv, '"' );
1866                                 assert( s != NULL );
1867                                 /* move the trailing quote of argv[0] to the end */
1868                                 AC_MEMCPY( s, s + 1, bv.bv_len - ( s - bv.bv_val ) );
1869                                 bv.bv_val[ bv.bv_len - 1 ] = '"';
1870
1871                         } else {
1872                                 ber_str2bv( c->argv[ 0 ], 0, 1, &bv );
1873                         }
1874                         
1875                         ber_bvarray_add( &authz_rewrites, &bv );
1876                         }
1877                         break;
1878 #endif
1879
1880
1881                 default:
1882                         Debug( LDAP_DEBUG_ANY,
1883                                 "%s: unknown CFG_TYPE %d.\n",
1884                                 c->log, c->type, 0 );
1885                         return 1;
1886
1887         }
1888         return(0);
1889 }
1890
1891
1892 static int
1893 config_fname(ConfigArgs *c) {
1894         if(c->op == SLAP_CONFIG_EMIT) {
1895                 if (c->private) {
1896                         ConfigFile *cf = c->private;
1897                         value_add_one( &c->rvalue_vals, &cf->c_file );
1898                         return 0;
1899                 }
1900                 return 1;
1901         }
1902         return(0);
1903 }
1904
1905 static int
1906 config_cfdir(ConfigArgs *c) {
1907         if(c->op == SLAP_CONFIG_EMIT) {
1908                 if ( !BER_BVISEMPTY( &cfdir )) {
1909                         value_add_one( &c->rvalue_vals, &cfdir );
1910                         return 0;
1911                 }
1912                 return 1;
1913         }
1914         return(0);
1915 }
1916
1917 static int
1918 config_search_base(ConfigArgs *c) {
1919         if(c->op == SLAP_CONFIG_EMIT) {
1920                 int rc = 1;
1921                 if (!BER_BVISEMPTY(&default_search_base)) {
1922                         value_add_one(&c->rvalue_vals, &default_search_base);
1923                         value_add_one(&c->rvalue_nvals, &default_search_nbase);
1924                         rc = 0;
1925                 }
1926                 return rc;
1927         } else if( c->op == LDAP_MOD_DELETE ) {
1928                 ch_free( default_search_base.bv_val );
1929                 ch_free( default_search_nbase.bv_val );
1930                 BER_BVZERO( &default_search_base );
1931                 BER_BVZERO( &default_search_nbase );
1932                 return 0;
1933         }
1934
1935         if(c->bi || c->be != frontendDB) {
1936                 Debug(LDAP_DEBUG_ANY, "%s: defaultSearchBase line must appear "
1937                         "prior to any backend or database definition\n",
1938                         c->log, 0, 0);
1939                 return(1);
1940         }
1941
1942         if(default_search_nbase.bv_len) {
1943                 free(default_search_base.bv_val);
1944                 free(default_search_nbase.bv_val);
1945         }
1946
1947         default_search_base = c->value_dn;
1948         default_search_nbase = c->value_ndn;
1949         return(0);
1950 }
1951
1952 /* For RE23 compatibility we allow this in the global entry
1953  * but we now defer it to the frontend entry to allow modules
1954  * to load new hash types.
1955  */
1956 static int
1957 config_passwd_hash(ConfigArgs *c) {
1958         int i;
1959         if (c->op == SLAP_CONFIG_EMIT) {
1960                 struct berval bv;
1961                 /* Don't generate it in the global entry */
1962                 if ( c->table == Cft_Global )
1963                         return 1;
1964                 for (i=0; default_passwd_hash && default_passwd_hash[i]; i++) {
1965                         ber_str2bv(default_passwd_hash[i], 0, 0, &bv);
1966                         value_add_one(&c->rvalue_vals, &bv);
1967                 }
1968                 return i ? 0 : 1;
1969         } else if ( c->op == LDAP_MOD_DELETE ) {
1970                 /* Deleting from global is a no-op, only the frontendDB entry matters */
1971                 if ( c->table == Cft_Global )
1972                         return 0;
1973                 if ( c->valx < 0 ) {
1974                         ldap_charray_free( default_passwd_hash );
1975                         default_passwd_hash = NULL;
1976                 } else {
1977                         i = c->valx;
1978                         ch_free( default_passwd_hash[i] );
1979                         for (; default_passwd_hash[i]; i++ )
1980                                 default_passwd_hash[i] = default_passwd_hash[i+1];
1981                 }
1982                 return 0;
1983         }
1984         for(i = 1; i < c->argc; i++) {
1985                 if(!lutil_passwd_scheme(c->argv[i])) {
1986                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> scheme not available", c->argv[0] );
1987                         Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
1988                                 c->log, c->cr_msg, c->argv[i]);
1989                 } else {
1990                         ldap_charray_add(&default_passwd_hash, c->argv[i]);
1991                 }
1992         }
1993         if(!default_passwd_hash) {
1994                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> no valid hashes found", c->argv[0] );
1995                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
1996                         c->log, c->cr_msg, 0 );
1997                 return(1);
1998         }
1999         return(0);
2000 }
2001
2002 static int
2003 config_schema_dn(ConfigArgs *c) {
2004         if ( c->op == SLAP_CONFIG_EMIT ) {
2005                 int rc = 1;
2006                 if ( !BER_BVISEMPTY( &c->be->be_schemadn )) {
2007                         value_add_one(&c->rvalue_vals, &c->be->be_schemadn);
2008                         value_add_one(&c->rvalue_nvals, &c->be->be_schemandn);
2009                         rc = 0;
2010                 }
2011                 return rc;
2012         } else if ( c->op == LDAP_MOD_DELETE ) {
2013                 ch_free( c->be->be_schemadn.bv_val );
2014                 ch_free( c->be->be_schemandn.bv_val );
2015                 BER_BVZERO( &c->be->be_schemadn );
2016                 BER_BVZERO( &c->be->be_schemandn );
2017                 return 0;
2018         }
2019         ch_free( c->be->be_schemadn.bv_val );
2020         ch_free( c->be->be_schemandn.bv_val );
2021         c->be->be_schemadn = c->value_dn;
2022         c->be->be_schemandn = c->value_ndn;
2023         return(0);
2024 }
2025
2026 static int
2027 config_sizelimit(ConfigArgs *c) {
2028         int i, rc = 0;
2029         struct slap_limits_set *lim = &c->be->be_def_limit;
2030         if (c->op == SLAP_CONFIG_EMIT) {
2031                 char buf[8192];
2032                 struct berval bv;
2033                 bv.bv_val = buf;
2034                 bv.bv_len = 0;
2035                 limits_unparse_one( lim, SLAP_LIMIT_SIZE, &bv, sizeof( buf ) );
2036                 if ( !BER_BVISEMPTY( &bv ))
2037                         value_add_one( &c->rvalue_vals, &bv );
2038                 else
2039                         rc = 1;
2040                 return rc;
2041         } else if ( c->op == LDAP_MOD_DELETE ) {
2042                 /* Reset to defaults */
2043                 lim->lms_s_soft = SLAPD_DEFAULT_SIZELIMIT;
2044                 lim->lms_s_hard = 0;
2045                 lim->lms_s_unchecked = -1;
2046                 lim->lms_s_pr = 0;
2047                 lim->lms_s_pr_hide = 0;
2048                 lim->lms_s_pr_total = 0;
2049                 return 0;
2050         }
2051         for(i = 1; i < c->argc; i++) {
2052                 if(!strncasecmp(c->argv[i], "size", 4)) {
2053                         rc = limits_parse_one(c->argv[i], lim);
2054                         if ( rc ) {
2055                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to parse value", c->argv[0] );
2056                                 Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2057                                         c->log, c->cr_msg, c->argv[i]);
2058                                 return(1);
2059                         }
2060                 } else {
2061                         if(!strcasecmp(c->argv[i], "unlimited")) {
2062                                 lim->lms_s_soft = -1;
2063                         } else {
2064                                 if ( lutil_atoix( &lim->lms_s_soft, c->argv[i], 0 ) != 0 ) {
2065                                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to parse limit", c->argv[0]);
2066                                         Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2067                                                 c->log, c->cr_msg, c->argv[i]);
2068                                         return(1);
2069                                 }
2070                         }
2071                         lim->lms_s_hard = 0;
2072                 }
2073         }
2074         return(0);
2075 }
2076
2077 static int
2078 config_timelimit(ConfigArgs *c) {
2079         int i, rc = 0;
2080         struct slap_limits_set *lim = &c->be->be_def_limit;
2081         if (c->op == SLAP_CONFIG_EMIT) {
2082                 char buf[8192];
2083                 struct berval bv;
2084                 bv.bv_val = buf;
2085                 bv.bv_len = 0;
2086                 limits_unparse_one( lim, SLAP_LIMIT_TIME, &bv, sizeof( buf ) );
2087                 if ( !BER_BVISEMPTY( &bv ))
2088                         value_add_one( &c->rvalue_vals, &bv );
2089                 else
2090                         rc = 1;
2091                 return rc;
2092         } else if ( c->op == LDAP_MOD_DELETE ) {
2093                 /* Reset to defaults */
2094                 lim->lms_t_soft = SLAPD_DEFAULT_TIMELIMIT;
2095                 lim->lms_t_hard = 0;
2096                 return 0;
2097         }
2098         for(i = 1; i < c->argc; i++) {
2099                 if(!strncasecmp(c->argv[i], "time", 4)) {
2100                         rc = limits_parse_one(c->argv[i], lim);
2101                         if ( rc ) {
2102                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to parse value", c->argv[0] );
2103                                 Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2104                                         c->log, c->cr_msg, c->argv[i]);
2105                                 return(1);
2106                         }
2107                 } else {
2108                         if(!strcasecmp(c->argv[i], "unlimited")) {
2109                                 lim->lms_t_soft = -1;
2110                         } else {
2111                                 if ( lutil_atoix( &lim->lms_t_soft, c->argv[i], 0 ) != 0 ) {
2112                                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to parse limit", c->argv[0]);
2113                                         Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2114                                                 c->log, c->cr_msg, c->argv[i]);
2115                                         return(1);
2116                                 }
2117                         }
2118                         lim->lms_t_hard = 0;
2119                 }
2120         }
2121         return(0);
2122 }
2123
2124 static int
2125 config_overlay(ConfigArgs *c) {
2126         if (c->op == SLAP_CONFIG_EMIT) {
2127                 return 1;
2128         } else if ( c->op == LDAP_MOD_DELETE ) {
2129                 assert(0);
2130         }
2131         if(c->argv[1][0] == '-' && overlay_config(c->be, &c->argv[1][1],
2132                 c->valx, &c->bi)) {
2133                 /* log error */
2134                 Debug( LDAP_DEBUG_ANY,
2135                         "%s: (optional) %s overlay \"%s\" configuration failed.\n",
2136                         c->log, c->be == frontendDB ? "global " : "", &c->argv[1][1]);
2137                 return 1;
2138         } else if(overlay_config(c->be, c->argv[1], c->valx, &c->bi)) {
2139                 return(1);
2140         }
2141         return(0);
2142 }
2143
2144 static int
2145 config_subordinate(ConfigArgs *c)
2146 {
2147         int rc = 1;
2148         int advertise;
2149
2150         switch( c->op ) {
2151         case SLAP_CONFIG_EMIT:
2152                 if ( SLAP_GLUE_SUBORDINATE( c->be )) {
2153                         struct berval bv;
2154
2155                         bv.bv_val = SLAP_GLUE_ADVERTISE( c->be ) ? "advertise" : "TRUE";
2156                         bv.bv_len = SLAP_GLUE_ADVERTISE( c->be ) ? STRLENOF("advertise") :
2157                                 STRLENOF("TRUE");
2158
2159                         value_add_one( &c->rvalue_vals, &bv );
2160                         rc = 0;
2161                 }
2162                 break;
2163         case LDAP_MOD_DELETE:
2164                 if ( !c->line  || strcasecmp( c->line, "advertise" )) {
2165                         glue_sub_del( c->be );
2166                 } else {
2167                         SLAP_DBFLAGS( c->be ) &= ~SLAP_DBFLAG_GLUE_ADVERTISE;
2168                 }
2169                 rc = 0;
2170                 break;
2171         case LDAP_MOD_ADD:
2172         case SLAP_CONFIG_ADD:
2173                 advertise = ( c->argc == 2 && !strcasecmp( c->argv[1], "advertise" ));
2174                 rc = glue_sub_add( c->be, advertise, CONFIG_ONLINE_ADD( c ));
2175                 break;
2176         }
2177         return rc;
2178 }
2179
2180 static int
2181 config_suffix(ConfigArgs *c)
2182 {
2183         Backend *tbe;
2184         struct berval pdn, ndn;
2185         char    *notallowed = NULL;
2186
2187         if ( c->be == frontendDB ) {
2188                 notallowed = "frontend";
2189
2190         } else if ( SLAP_MONITOR(c->be) ) {
2191                 notallowed = "monitor";
2192
2193         } else if ( SLAP_CONFIG(c->be) ) {
2194                 notallowed = "config";
2195         }
2196
2197         if ( notallowed != NULL ) {
2198                 char    buf[ SLAP_TEXT_BUFLEN ] = { '\0' };
2199
2200                 switch ( c->op ) {
2201                 case LDAP_MOD_ADD:
2202                 case LDAP_MOD_DELETE:
2203                 case LDAP_MOD_REPLACE:
2204                 case LDAP_MOD_INCREMENT:
2205                 case SLAP_CONFIG_ADD:
2206                         if ( !BER_BVISNULL( &c->value_dn ) ) {
2207                                 snprintf( buf, sizeof( buf ), "<%s> ",
2208                                                 c->value_dn.bv_val );
2209                         }
2210
2211                         Debug(LDAP_DEBUG_ANY,
2212                                 "%s: suffix %snot allowed in %s database.\n",
2213                                 c->log, buf, notallowed );
2214                         break;
2215
2216                 case SLAP_CONFIG_EMIT:
2217                         /* don't complain when emitting... */
2218                         break;
2219
2220                 default:
2221                         /* FIXME: don't know what values may be valid;
2222                          * please remove assertion, or add legal values
2223                          * to either block */
2224                         assert( 0 );
2225                         break;
2226                 }
2227
2228                 return 1;
2229         }
2230
2231         if (c->op == SLAP_CONFIG_EMIT) {
2232                 if ( c->be->be_suffix == NULL
2233                                 || BER_BVISNULL( &c->be->be_suffix[0] ) )
2234                 {
2235                         return 1;
2236                 } else {
2237                         value_add( &c->rvalue_vals, c->be->be_suffix );
2238                         value_add( &c->rvalue_nvals, c->be->be_nsuffix );
2239                         return 0;
2240                 }
2241         } else if ( c->op == LDAP_MOD_DELETE ) {
2242                 if ( c->valx < 0 ) {
2243                         ber_bvarray_free( c->be->be_suffix );
2244                         ber_bvarray_free( c->be->be_nsuffix );
2245                         c->be->be_suffix = NULL;
2246                         c->be->be_nsuffix = NULL;
2247                 } else {
2248                         int i = c->valx;
2249                         ch_free( c->be->be_suffix[i].bv_val );
2250                         ch_free( c->be->be_nsuffix[i].bv_val );
2251                         do {
2252                                 c->be->be_suffix[i] = c->be->be_suffix[i+1];
2253                                 c->be->be_nsuffix[i] = c->be->be_nsuffix[i+1];
2254                                 i++;
2255                         } while ( !BER_BVISNULL( &c->be->be_suffix[i] ) );
2256                 }
2257                 return 0;
2258         }
2259
2260 #ifdef SLAPD_MONITOR_DN
2261         if(!strcasecmp(c->argv[1], SLAPD_MONITOR_DN)) {
2262                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> DN is reserved for monitoring slapd",
2263                         c->argv[0] );
2264                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
2265                         c->log, c->cr_msg, SLAPD_MONITOR_DN);
2266                 return(1);
2267         }
2268 #endif
2269
2270         if (SLAP_DB_ONE_SUFFIX( c->be ) && c->be->be_suffix ) {
2271                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> Only one suffix is allowed on this %s backend",
2272                         c->argv[0], c->be->bd_info->bi_type );
2273                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
2274                         c->log, c->cr_msg, 0);
2275                 return(1);
2276         }
2277
2278         pdn = c->value_dn;
2279         ndn = c->value_ndn;
2280
2281         if (SLAP_DBHIDDEN( c->be ))
2282                 tbe = NULL;
2283         else
2284                 tbe = select_backend(&ndn, 0);
2285         if(tbe == c->be) {
2286                 Debug( LDAP_DEBUG_ANY, "%s: suffix already served by this backend!.\n",
2287                         c->log, 0, 0);
2288                 return 1;
2289                 free(pdn.bv_val);
2290                 free(ndn.bv_val);
2291         } else if(tbe) {
2292                 BackendDB *b2 = tbe;
2293
2294                 /* Does tbe precede be? */
2295                 while (( b2 = LDAP_STAILQ_NEXT(b2, be_next )) && b2 && b2 != c->be );
2296
2297                 if ( b2 ) {
2298                         char    *type = tbe->bd_info->bi_type;
2299
2300                         if ( overlay_is_over( tbe ) ) {
2301                                 slap_overinfo   *oi = (slap_overinfo *)tbe->bd_info->bi_private;
2302                                 type = oi->oi_orig->bi_type;
2303                         }
2304
2305                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> namingContext \"%s\" "
2306                                 "already served by a preceding %s database",
2307                                 c->argv[0], pdn.bv_val, type );
2308                         Debug(LDAP_DEBUG_ANY, "%s: %s serving namingContext \"%s\"\n",
2309                                 c->log, c->cr_msg, tbe->be_suffix[0].bv_val);
2310                         free(pdn.bv_val);
2311                         free(ndn.bv_val);
2312                         return(1);
2313                 }
2314         }
2315         if(pdn.bv_len == 0 && default_search_nbase.bv_len) {
2316                 Debug(LDAP_DEBUG_ANY, "%s: suffix DN empty and default search "
2317                         "base provided \"%s\" (assuming okay)\n",
2318                         c->log, default_search_base.bv_val, 0);
2319         }
2320         ber_bvarray_add(&c->be->be_suffix, &pdn);
2321         ber_bvarray_add(&c->be->be_nsuffix, &ndn);
2322         return(0);
2323 }
2324
2325 static int
2326 config_rootdn(ConfigArgs *c) {
2327         if (c->op == SLAP_CONFIG_EMIT) {
2328                 if ( !BER_BVISNULL( &c->be->be_rootdn )) {
2329                         value_add_one(&c->rvalue_vals, &c->be->be_rootdn);
2330                         value_add_one(&c->rvalue_nvals, &c->be->be_rootndn);
2331                         return 0;
2332                 } else {
2333                         return 1;
2334                 }
2335         } else if ( c->op == LDAP_MOD_DELETE ) {
2336                 ch_free( c->be->be_rootdn.bv_val );
2337                 ch_free( c->be->be_rootndn.bv_val );
2338                 BER_BVZERO( &c->be->be_rootdn );
2339                 BER_BVZERO( &c->be->be_rootndn );
2340                 return 0;
2341         }
2342         if ( !BER_BVISNULL( &c->be->be_rootdn )) {
2343                 ch_free( c->be->be_rootdn.bv_val );
2344                 ch_free( c->be->be_rootndn.bv_val );
2345         }
2346         c->be->be_rootdn = c->value_dn;
2347         c->be->be_rootndn = c->value_ndn;
2348         return(0);
2349 }
2350
2351 static int
2352 config_rootpw(ConfigArgs *c) {
2353         Backend *tbe;
2354
2355         if (c->op == SLAP_CONFIG_EMIT) {
2356                 if (!BER_BVISEMPTY(&c->be->be_rootpw)) {
2357                         /* don't copy, because "rootpw" is marked
2358                          * as CFG_BERVAL */
2359                         c->value_bv = c->be->be_rootpw;
2360                         return 0;
2361                 }
2362                 return 1;
2363         } else if ( c->op == LDAP_MOD_DELETE ) {
2364                 ch_free( c->be->be_rootpw.bv_val );
2365                 BER_BVZERO( &c->be->be_rootpw );
2366                 return 0;
2367         }
2368
2369         tbe = select_backend(&c->be->be_rootndn, 0);
2370         if(tbe != c->be) {
2371                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> can only be set when rootdn is under suffix",
2372                         c->argv[0] );
2373                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
2374                         c->log, c->cr_msg, 0);
2375                 return(1);
2376         }
2377         if ( !BER_BVISNULL( &c->be->be_rootpw ))
2378                 ch_free( c->be->be_rootpw.bv_val );
2379         c->be->be_rootpw = c->value_bv;
2380         return(0);
2381 }
2382
2383 static int
2384 config_restrict(ConfigArgs *c) {
2385         slap_mask_t restrictops = 0;
2386         int i;
2387         slap_verbmasks restrictable_ops[] = {
2388                 { BER_BVC("bind"),              SLAP_RESTRICT_OP_BIND },
2389                 { BER_BVC("add"),               SLAP_RESTRICT_OP_ADD },
2390                 { BER_BVC("modify"),            SLAP_RESTRICT_OP_MODIFY },
2391                 { BER_BVC("rename"),            SLAP_RESTRICT_OP_RENAME },
2392                 { BER_BVC("modrdn"),            0 },
2393                 { BER_BVC("delete"),            SLAP_RESTRICT_OP_DELETE },
2394                 { BER_BVC("search"),            SLAP_RESTRICT_OP_SEARCH },
2395                 { BER_BVC("compare"),           SLAP_RESTRICT_OP_COMPARE },
2396                 { BER_BVC("read"),              SLAP_RESTRICT_OP_READS },
2397                 { BER_BVC("write"),             SLAP_RESTRICT_OP_WRITES },
2398                 { BER_BVC("extended"),          SLAP_RESTRICT_OP_EXTENDED },
2399                 { BER_BVC("extended=" LDAP_EXOP_START_TLS ),            SLAP_RESTRICT_EXOP_START_TLS },
2400                 { BER_BVC("extended=" LDAP_EXOP_MODIFY_PASSWD ),        SLAP_RESTRICT_EXOP_MODIFY_PASSWD },
2401                 { BER_BVC("extended=" LDAP_EXOP_X_WHO_AM_I ),           SLAP_RESTRICT_EXOP_WHOAMI },
2402                 { BER_BVC("extended=" LDAP_EXOP_X_CANCEL ),             SLAP_RESTRICT_EXOP_CANCEL },
2403                 { BER_BVC("all"),               SLAP_RESTRICT_OP_ALL },
2404                 { BER_BVNULL,   0 }
2405         };
2406
2407         if (c->op == SLAP_CONFIG_EMIT) {
2408                 return mask_to_verbs( restrictable_ops, c->be->be_restrictops,
2409                         &c->rvalue_vals );
2410         } else if ( c->op == LDAP_MOD_DELETE ) {
2411                 if ( !c->line ) {
2412                         c->be->be_restrictops = 0;
2413                 } else {
2414                         restrictops = verb_to_mask( c->line, restrictable_ops );
2415                         c->be->be_restrictops ^= restrictops;
2416                 }
2417                 return 0;
2418         }
2419         i = verbs_to_mask( c->argc, c->argv, restrictable_ops, &restrictops );
2420         if ( i ) {
2421                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown operation", c->argv[0] );
2422                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
2423                         c->log, c->cr_msg, c->argv[i]);
2424                 return(1);
2425         }
2426         if ( restrictops & SLAP_RESTRICT_OP_EXTENDED )
2427                 restrictops &= ~SLAP_RESTRICT_EXOP_MASK;
2428         c->be->be_restrictops |= restrictops;
2429         return(0);
2430 }
2431
2432 static int
2433 config_allows(ConfigArgs *c) {
2434         slap_mask_t allows = 0;
2435         int i;
2436         slap_verbmasks allowable_ops[] = {
2437                 { BER_BVC("bind_v2"),           SLAP_ALLOW_BIND_V2 },
2438                 { BER_BVC("bind_anon_cred"),    SLAP_ALLOW_BIND_ANON_CRED },
2439                 { BER_BVC("bind_anon_dn"),      SLAP_ALLOW_BIND_ANON_DN },
2440                 { BER_BVC("update_anon"),       SLAP_ALLOW_UPDATE_ANON },
2441                 { BER_BVC("proxy_authz_anon"),  SLAP_ALLOW_PROXY_AUTHZ_ANON },
2442                 { BER_BVNULL,   0 }
2443         };
2444         if (c->op == SLAP_CONFIG_EMIT) {
2445                 return mask_to_verbs( allowable_ops, global_allows, &c->rvalue_vals );
2446         } else if ( c->op == LDAP_MOD_DELETE ) {
2447                 if ( !c->line ) {
2448                         global_allows = 0;
2449                 } else {
2450                         allows = verb_to_mask( c->line, allowable_ops );
2451                         global_allows ^= allows;
2452                 }
2453                 return 0;
2454         }
2455         i = verbs_to_mask(c->argc, c->argv, allowable_ops, &allows);
2456         if ( i ) {
2457                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown feature", c->argv[0] );
2458                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
2459                         c->log, c->cr_msg, c->argv[i]);
2460                 return(1);
2461         }
2462         global_allows |= allows;
2463         return(0);
2464 }
2465
2466 static int
2467 config_disallows(ConfigArgs *c) {
2468         slap_mask_t disallows = 0;
2469         int i;
2470         slap_verbmasks disallowable_ops[] = {
2471                 { BER_BVC("bind_anon"),         SLAP_DISALLOW_BIND_ANON },
2472                 { BER_BVC("bind_simple"),       SLAP_DISALLOW_BIND_SIMPLE },
2473                 { BER_BVC("tls_2_anon"),                SLAP_DISALLOW_TLS_2_ANON },
2474                 { BER_BVC("tls_authc"),         SLAP_DISALLOW_TLS_AUTHC },
2475                 { BER_BVNULL, 0 }
2476         };
2477         if (c->op == SLAP_CONFIG_EMIT) {
2478                 return mask_to_verbs( disallowable_ops, global_disallows, &c->rvalue_vals );
2479         } else if ( c->op == LDAP_MOD_DELETE ) {
2480                 if ( !c->line ) {
2481                         global_disallows = 0;
2482                 } else {
2483                         disallows = verb_to_mask( c->line, disallowable_ops );
2484                         global_disallows ^= disallows;
2485                 }
2486                 return 0;
2487         }
2488         i = verbs_to_mask(c->argc, c->argv, disallowable_ops, &disallows);
2489         if ( i ) {
2490                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown feature", c->argv[0] );
2491                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
2492                         c->log, c->cr_msg, c->argv[i]);
2493                 return(1);
2494         }
2495         global_disallows |= disallows;
2496         return(0);
2497 }
2498
2499 static int
2500 config_requires(ConfigArgs *c) {
2501         slap_mask_t requires = frontendDB->be_requires;
2502         int i, argc = c->argc;
2503         char **argv = c->argv;
2504
2505         slap_verbmasks requires_ops[] = {
2506                 { BER_BVC("bind"),              SLAP_REQUIRE_BIND },
2507                 { BER_BVC("LDAPv3"),            SLAP_REQUIRE_LDAP_V3 },
2508                 { BER_BVC("authc"),             SLAP_REQUIRE_AUTHC },
2509                 { BER_BVC("sasl"),              SLAP_REQUIRE_SASL },
2510                 { BER_BVC("strong"),            SLAP_REQUIRE_STRONG },
2511                 { BER_BVNULL, 0 }
2512         };
2513         if (c->op == SLAP_CONFIG_EMIT) {
2514                 return mask_to_verbs( requires_ops, c->be->be_requires, &c->rvalue_vals );
2515         } else if ( c->op == LDAP_MOD_DELETE ) {
2516                 if ( !c->line ) {
2517                         c->be->be_requires = 0;
2518                 } else {
2519                         requires = verb_to_mask( c->line, requires_ops );
2520                         c->be->be_requires ^= requires;
2521                 }
2522                 return 0;
2523         }
2524         /* "none" can only be first, to wipe out default/global values */
2525         if ( strcasecmp( c->argv[ 1 ], "none" ) == 0 ) {
2526                 argv++;
2527                 argc--;
2528                 requires = 0;
2529         }
2530         i = verbs_to_mask(argc, argv, requires_ops, &requires);
2531         if ( i ) {
2532                 if (strcasecmp( c->argv[ i ], "none" ) == 0 ) {
2533                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> \"none\" (#%d) must be listed first", c->argv[0], i - 1 );
2534                         Debug(LDAP_DEBUG_ANY, "%s: %s\n",
2535                                 c->log, c->cr_msg, 0);
2536                 } else {
2537                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown feature #%d", c->argv[0], i - 1 );
2538                         Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2539                                 c->log, c->cr_msg, c->argv[i]);
2540                 }
2541                 return(1);
2542         }
2543         c->be->be_requires = requires;
2544         return(0);
2545 }
2546
2547 static slap_verbmasks   *loglevel_ops;
2548
2549 static int
2550 loglevel_init( void )
2551 {
2552         slap_verbmasks  lo[] = {
2553                 { BER_BVC("Any"),       -1 },
2554                 { BER_BVC("Trace"),     LDAP_DEBUG_TRACE },
2555                 { BER_BVC("Packets"),   LDAP_DEBUG_PACKETS },
2556                 { BER_BVC("Args"),      LDAP_DEBUG_ARGS },
2557                 { BER_BVC("Conns"),     LDAP_DEBUG_CONNS },
2558                 { BER_BVC("BER"),       LDAP_DEBUG_BER },
2559                 { BER_BVC("Filter"),    LDAP_DEBUG_FILTER },
2560                 { BER_BVC("Config"),    LDAP_DEBUG_CONFIG },
2561                 { BER_BVC("ACL"),       LDAP_DEBUG_ACL },
2562                 { BER_BVC("Stats"),     LDAP_DEBUG_STATS },
2563                 { BER_BVC("Stats2"),    LDAP_DEBUG_STATS2 },
2564                 { BER_BVC("Shell"),     LDAP_DEBUG_SHELL },
2565                 { BER_BVC("Parse"),     LDAP_DEBUG_PARSE },
2566 #if 0   /* no longer used (nor supported) */
2567                 { BER_BVC("Cache"),     LDAP_DEBUG_CACHE },
2568                 { BER_BVC("Index"),     LDAP_DEBUG_INDEX },
2569 #endif
2570                 { BER_BVC("Sync"),      LDAP_DEBUG_SYNC },
2571                 { BER_BVC("None"),      LDAP_DEBUG_NONE },
2572                 { BER_BVNULL,           0 }
2573         };
2574
2575         return slap_verbmasks_init( &loglevel_ops, lo );
2576 }
2577
2578 static void
2579 loglevel_destroy( void )
2580 {
2581         if ( loglevel_ops ) {
2582                 (void)slap_verbmasks_destroy( loglevel_ops );
2583         }
2584         loglevel_ops = NULL;
2585 }
2586
2587 static slap_mask_t      loglevel_ignore[] = { -1, 0 };
2588
2589 int
2590 slap_loglevel_register( slap_mask_t m, struct berval *s )
2591 {
2592         int     rc;
2593
2594         if ( loglevel_ops == NULL ) {
2595                 loglevel_init();
2596         }
2597
2598         rc = slap_verbmasks_append( &loglevel_ops, m, s, loglevel_ignore );
2599
2600         if ( rc != 0 ) {
2601                 Debug( LDAP_DEBUG_ANY, "slap_loglevel_register(%lu, \"%s\") failed\n",
2602                         m, s->bv_val, 0 );
2603         }
2604
2605         return rc;
2606 }
2607
2608 int
2609 slap_loglevel_get( struct berval *s, int *l )
2610 {
2611         int             rc;
2612         slap_mask_t     m, i;
2613
2614         if ( loglevel_ops == NULL ) {
2615                 loglevel_init();
2616         }
2617
2618         for ( m = 0, i = 1; !BER_BVISNULL( &loglevel_ops[ i ].word ); i++ ) {
2619                 m |= loglevel_ops[ i ].mask;
2620         }
2621
2622         for ( i = 1; m & i; i <<= 1 )
2623                 ;
2624
2625         if ( i == 0 ) {
2626                 return -1;
2627         }
2628
2629         rc = slap_verbmasks_append( &loglevel_ops, i, s, loglevel_ignore );
2630
2631         if ( rc != 0 ) {
2632                 Debug( LDAP_DEBUG_ANY, "slap_loglevel_get(%lu, \"%s\") failed\n",
2633                         i, s->bv_val, 0 );
2634
2635         } else {
2636                 *l = i;
2637         }
2638
2639         return rc;
2640 }
2641
2642 int
2643 str2loglevel( const char *s, int *l )
2644 {
2645         int     i;
2646
2647         if ( loglevel_ops == NULL ) {
2648                 loglevel_init();
2649         }
2650
2651         i = verb_to_mask( s, loglevel_ops );
2652
2653         if ( BER_BVISNULL( &loglevel_ops[ i ].word ) ) {
2654                 return -1;
2655         }
2656
2657         *l = loglevel_ops[ i ].mask;
2658
2659         return 0;
2660 }
2661
2662 const char *
2663 loglevel2str( int l )
2664 {
2665         struct berval   bv = BER_BVNULL;
2666
2667         loglevel2bv( l, &bv );
2668
2669         return bv.bv_val;
2670 }
2671
2672 int
2673 loglevel2bv( int l, struct berval *bv )
2674 {
2675         if ( loglevel_ops == NULL ) {
2676                 loglevel_init();
2677         }
2678
2679         BER_BVZERO( bv );
2680
2681         return enum_to_verb( loglevel_ops, l, bv ) == -1;
2682 }
2683
2684 int
2685 loglevel2bvarray( int l, BerVarray *bva )
2686 {
2687         if ( loglevel_ops == NULL ) {
2688                 loglevel_init();
2689         }
2690
2691         return mask_to_verbs( loglevel_ops, l, bva );
2692 }
2693
2694 int
2695 loglevel_print( FILE *out )
2696 {
2697         int     i;
2698
2699         if ( loglevel_ops == NULL ) {
2700                 loglevel_init();
2701         }
2702
2703         fprintf( out, "Installed log subsystems:\n\n" );
2704         for ( i = 0; !BER_BVISNULL( &loglevel_ops[ i ].word ); i++ ) {
2705                 fprintf( out, "\t%-30s (%lu)\n",
2706                         loglevel_ops[ i ].word.bv_val,
2707                         loglevel_ops[ i ].mask );
2708         }
2709
2710         fprintf( out, "\nNOTE: custom log subsystems may be later installed "
2711                 "by specific code\n\n" );
2712
2713         return 0;
2714 }
2715
2716 static int config_syslog;
2717
2718 static int
2719 config_loglevel(ConfigArgs *c) {
2720         int i;
2721
2722         if ( loglevel_ops == NULL ) {
2723                 loglevel_init();
2724         }
2725
2726         if (c->op == SLAP_CONFIG_EMIT) {
2727                 /* Get default or commandline slapd setting */
2728                 if ( ldap_syslog && !config_syslog )
2729                         config_syslog = ldap_syslog;
2730                 return loglevel2bvarray( config_syslog, &c->rvalue_vals );
2731
2732         } else if ( c->op == LDAP_MOD_DELETE ) {
2733                 if ( !c->line ) {
2734                         config_syslog = 0;
2735                 } else {
2736                         int level = verb_to_mask( c->line, loglevel_ops );
2737                         config_syslog ^= level;
2738                 }
2739                 if ( slapMode & SLAP_SERVER_MODE ) {
2740                         ldap_syslog = config_syslog;
2741                 }
2742                 return 0;
2743         }
2744
2745         for( i=1; i < c->argc; i++ ) {
2746                 int     level;
2747
2748                 if ( isdigit((unsigned char)c->argv[i][0]) || c->argv[i][0] == '-' ) {
2749                         if( lutil_atoi( &level, c->argv[i] ) != 0 ) {
2750                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to parse level", c->argv[0] );
2751                                 Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2752                                         c->log, c->cr_msg, c->argv[i]);
2753                                 return( 1 );
2754                         }
2755                 } else {
2756                         if ( str2loglevel( c->argv[i], &level ) ) {
2757                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown level", c->argv[0] );
2758                                 Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2759                                         c->log, c->cr_msg, c->argv[i]);
2760                                 return( 1 );
2761                         }
2762                 }
2763                 /* Explicitly setting a zero clears all the levels */
2764                 if ( level )
2765                         config_syslog |= level;
2766                 else
2767                         config_syslog = 0;
2768         }
2769         if ( slapMode & SLAP_SERVER_MODE ) {
2770                 ldap_syslog = config_syslog;
2771         }
2772         return(0);
2773 }
2774
2775 static int
2776 config_referral(ConfigArgs *c) {
2777         struct berval val;
2778         if (c->op == SLAP_CONFIG_EMIT) {
2779                 if ( default_referral ) {
2780                         value_add( &c->rvalue_vals, default_referral );
2781                         return 0;
2782                 } else {
2783                         return 1;
2784                 }
2785         } else if ( c->op == LDAP_MOD_DELETE ) {
2786                 if ( c->valx < 0 ) {
2787                         ber_bvarray_free( default_referral );
2788                         default_referral = NULL;
2789                 } else {
2790                         int i = c->valx;
2791                         ch_free( default_referral[i].bv_val );
2792                         for (; default_referral[i].bv_val; i++ )
2793                                 default_referral[i] = default_referral[i+1];
2794                 }
2795                 return 0;
2796         }
2797         if(validate_global_referral(c->argv[1])) {
2798                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid URL", c->argv[0] );
2799                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
2800                         c->log, c->cr_msg, c->argv[1]);
2801                 return(1);
2802         }
2803
2804         ber_str2bv(c->argv[1], 0, 0, &val);
2805         if(value_add_one(&default_referral, &val)) return(LDAP_OTHER);
2806         return(0);
2807 }
2808
2809 static struct {
2810         struct berval key;
2811         int off;
2812 } sec_keys[] = {
2813         { BER_BVC("ssf="), offsetof(slap_ssf_set_t, sss_ssf) },
2814         { BER_BVC("transport="), offsetof(slap_ssf_set_t, sss_transport) },
2815         { BER_BVC("tls="), offsetof(slap_ssf_set_t, sss_tls) },
2816         { BER_BVC("sasl="), offsetof(slap_ssf_set_t, sss_sasl) },
2817         { BER_BVC("update_ssf="), offsetof(slap_ssf_set_t, sss_update_ssf) },
2818         { BER_BVC("update_transport="), offsetof(slap_ssf_set_t, sss_update_transport) },
2819         { BER_BVC("update_tls="), offsetof(slap_ssf_set_t, sss_update_tls) },
2820         { BER_BVC("update_sasl="), offsetof(slap_ssf_set_t, sss_update_sasl) },
2821         { BER_BVC("simple_bind="), offsetof(slap_ssf_set_t, sss_simple_bind) },
2822         { BER_BVNULL, 0 }
2823 };
2824
2825 static int
2826 config_security(ConfigArgs *c) {
2827         slap_ssf_set_t *set = &c->be->be_ssf_set;
2828         char *next;
2829         int i, j;
2830         if (c->op == SLAP_CONFIG_EMIT) {
2831                 char numbuf[32];
2832                 struct berval bv;
2833                 slap_ssf_t *tgt;
2834                 int rc = 1;
2835
2836                 for (i=0; !BER_BVISNULL( &sec_keys[i].key ); i++) {
2837                         tgt = (slap_ssf_t *)((char *)set + sec_keys[i].off);
2838                         if ( *tgt ) {
2839                                 rc = 0;
2840                                 bv.bv_len = snprintf( numbuf, sizeof( numbuf ), "%u", *tgt );
2841                                 if ( bv.bv_len >= sizeof( numbuf ) ) {
2842                                         ber_bvarray_free_x( c->rvalue_vals, NULL );
2843                                         c->rvalue_vals = NULL;
2844                                         rc = 1;
2845                                         break;
2846                                 }
2847                                 bv.bv_len += sec_keys[i].key.bv_len;
2848                                 bv.bv_val = ch_malloc( bv.bv_len + 1);
2849                                 next = lutil_strcopy( bv.bv_val, sec_keys[i].key.bv_val );
2850                                 strcpy( next, numbuf );
2851                                 ber_bvarray_add( &c->rvalue_vals, &bv );
2852                         }
2853                 }
2854                 return rc;
2855         }
2856         for(i = 1; i < c->argc; i++) {
2857                 slap_ssf_t *tgt = NULL;
2858                 char *src = NULL;
2859                 for ( j=0; !BER_BVISNULL( &sec_keys[j].key ); j++ ) {
2860                         if(!strncasecmp(c->argv[i], sec_keys[j].key.bv_val,
2861                                 sec_keys[j].key.bv_len)) {
2862                                 src = c->argv[i] + sec_keys[j].key.bv_len;
2863                                 tgt = (slap_ssf_t *)((char *)set + sec_keys[j].off);
2864                                 break;
2865                         }
2866                 }
2867                 if ( !tgt ) {
2868                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown factor", c->argv[0] );
2869                         Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
2870                                 c->log, c->cr_msg, c->argv[i]);
2871                         return(1);
2872                 }
2873
2874                 if ( lutil_atou( tgt, src ) != 0 ) {
2875                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to parse factor", c->argv[0] );
2876                         Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2877                                 c->log, c->cr_msg, c->argv[i]);
2878                         return(1);
2879                 }
2880         }
2881         return(0);
2882 }
2883
2884 char *
2885 anlist_unparse( AttributeName *an, char *ptr, ber_len_t buflen ) {
2886         int comma = 0;
2887         char *start = ptr;
2888
2889         for (; !BER_BVISNULL( &an->an_name ); an++) {
2890                 /* if buflen == 0, assume the buffer size has been 
2891                  * already checked otherwise */
2892                 if ( buflen > 0 && buflen - ( ptr - start ) < comma + an->an_name.bv_len ) return NULL;
2893                 if ( comma ) *ptr++ = ',';
2894                 ptr = lutil_strcopy( ptr, an->an_name.bv_val );
2895                 comma = 1;
2896         }
2897         return ptr;
2898 }
2899
2900 static int
2901 config_updatedn(ConfigArgs *c) {
2902         if (c->op == SLAP_CONFIG_EMIT) {
2903                 if (!BER_BVISEMPTY(&c->be->be_update_ndn)) {
2904                         value_add_one(&c->rvalue_vals, &c->be->be_update_ndn);
2905                         value_add_one(&c->rvalue_nvals, &c->be->be_update_ndn);
2906                         return 0;
2907                 }
2908                 return 1;
2909         } else if ( c->op == LDAP_MOD_DELETE ) {
2910                 ch_free( c->be->be_update_ndn.bv_val );
2911                 BER_BVZERO( &c->be->be_update_ndn );
2912                 SLAP_DBFLAGS(c->be) ^= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SLURP_SHADOW);
2913                 return 0;
2914         }
2915         if(SLAP_SHADOW(c->be)) {
2916                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> database already shadowed", c->argv[0] );
2917                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
2918                         c->log, c->cr_msg, 0);
2919                 return(1);
2920         }
2921
2922         ber_memfree_x( c->value_dn.bv_val, NULL );
2923         if ( !BER_BVISNULL( &c->be->be_update_ndn ) ) {
2924                 ber_memfree_x( c->be->be_update_ndn.bv_val, NULL );
2925         }
2926         c->be->be_update_ndn = c->value_ndn;
2927         BER_BVZERO( &c->value_dn );
2928         BER_BVZERO( &c->value_ndn );
2929
2930         return config_slurp_shadow( c );
2931 }
2932
2933 int
2934 config_shadow( ConfigArgs *c, int flag )
2935 {
2936         char    *notallowed = NULL;
2937
2938         if ( c->be == frontendDB ) {
2939                 notallowed = "frontend";
2940
2941         } else if ( SLAP_MONITOR(c->be) ) {
2942                 notallowed = "monitor";
2943         }
2944
2945         if ( notallowed != NULL ) {
2946                 Debug( LDAP_DEBUG_ANY, "%s: %s database cannot be shadow.\n", c->log, notallowed, 0 );
2947                 return 1;
2948         }
2949
2950         SLAP_DBFLAGS(c->be) |= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SINGLE_SHADOW | flag);
2951
2952         return 0;
2953 }
2954
2955 static int
2956 config_updateref(ConfigArgs *c) {
2957         struct berval val;
2958         if (c->op == SLAP_CONFIG_EMIT) {
2959                 if ( c->be->be_update_refs ) {
2960                         value_add( &c->rvalue_vals, c->be->be_update_refs );
2961                         return 0;
2962                 } else {
2963                         return 1;
2964                 }
2965         } else if ( c->op == LDAP_MOD_DELETE ) {
2966                 if ( c->valx < 0 ) {
2967                         ber_bvarray_free( c->be->be_update_refs );
2968                         c->be->be_update_refs = NULL;
2969                 } else {
2970                         int i = c->valx;
2971                         ch_free( c->be->be_update_refs[i].bv_val );
2972                         for (; c->be->be_update_refs[i].bv_val; i++)
2973                                 c->be->be_update_refs[i] = c->be->be_update_refs[i+1];
2974                 }
2975                 return 0;
2976         }
2977         if(!SLAP_SHADOW(c->be) && !c->be->be_syncinfo) {
2978                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> must appear after syncrepl or updatedn",
2979                         c->argv[0] );
2980                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
2981                         c->log, c->cr_msg, 0);
2982                 return(1);
2983         }
2984
2985         if(validate_global_referral(c->argv[1])) {
2986                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid URL", c->argv[0] );
2987                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
2988                         c->log, c->cr_msg, c->argv[1]);
2989                 return(1);
2990         }
2991         ber_str2bv(c->argv[1], 0, 0, &val);
2992         if(value_add_one(&c->be->be_update_refs, &val)) return(LDAP_OTHER);
2993         return(0);
2994 }
2995
2996 static int
2997 config_obsolete(ConfigArgs *c) {
2998         if (c->op == SLAP_CONFIG_EMIT)
2999                 return 1;
3000
3001         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> keyword is obsolete (ignored)",
3002                 c->argv[0] );
3003         Debug(LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0);
3004         return(0);
3005 }
3006
3007 static int
3008 config_include(ConfigArgs *c) {
3009         int savelineno = c->lineno;
3010         int rc;
3011         ConfigFile *cf;
3012         ConfigFile *cfsave = cfn;
3013         ConfigFile *cf2 = NULL;
3014
3015         /* Leftover from RE23. No dynamic config for include files */
3016         if ( c->op == SLAP_CONFIG_EMIT || c->op == LDAP_MOD_DELETE )
3017                 return 1;
3018
3019         cf = ch_calloc( 1, sizeof(ConfigFile));
3020         if ( cfn->c_kids ) {
3021                 for (cf2=cfn->c_kids; cf2 && cf2->c_sibs; cf2=cf2->c_sibs) ;
3022                 cf2->c_sibs = cf;
3023         } else {
3024                 cfn->c_kids = cf;
3025         }
3026         cfn = cf;
3027         ber_str2bv( c->argv[1], 0, 1, &cf->c_file );
3028         rc = read_config_file(c->argv[1], c->depth + 1, c, config_back_cf_table);
3029         c->lineno = savelineno - 1;
3030         cfn = cfsave;
3031         if ( rc ) {
3032                 if ( cf2 ) cf2->c_sibs = NULL;
3033                 else cfn->c_kids = NULL;
3034                 ch_free( cf->c_file.bv_val );
3035                 ch_free( cf );
3036         } else {
3037                 c->private = cf;
3038         }
3039         return(rc);
3040 }
3041
3042 #ifdef HAVE_TLS
3043 static int
3044 config_tls_option(ConfigArgs *c) {
3045         int flag;
3046         LDAP *ld = slap_tls_ld;
3047         switch(c->type) {
3048         case CFG_TLS_RAND:      flag = LDAP_OPT_X_TLS_RANDOM_FILE;      ld = NULL; break;
3049         case CFG_TLS_CIPHER:    flag = LDAP_OPT_X_TLS_CIPHER_SUITE;     break;
3050         case CFG_TLS_CERT_FILE: flag = LDAP_OPT_X_TLS_CERTFILE;         break;  
3051         case CFG_TLS_CERT_KEY:  flag = LDAP_OPT_X_TLS_KEYFILE;          break;
3052         case CFG_TLS_CA_PATH:   flag = LDAP_OPT_X_TLS_CACERTDIR;        break;
3053         case CFG_TLS_CA_FILE:   flag = LDAP_OPT_X_TLS_CACERTFILE;       break;
3054         case CFG_TLS_DH_FILE:   flag = LDAP_OPT_X_TLS_DHFILE;   break;
3055 #ifdef HAVE_GNUTLS
3056         case CFG_TLS_CRL_FILE:  flag = LDAP_OPT_X_TLS_CRLFILE;  break;
3057 #endif
3058         default:                Debug(LDAP_DEBUG_ANY, "%s: "
3059                                         "unknown tls_option <0x%x>\n",
3060                                         c->log, c->type, 0);
3061                 return 1;
3062         }
3063         if (c->op == SLAP_CONFIG_EMIT) {
3064                 return ldap_pvt_tls_get_option( ld, flag, &c->value_string );
3065         } else if ( c->op == LDAP_MOD_DELETE ) {
3066                 return ldap_pvt_tls_set_option( ld, flag, NULL );
3067         }
3068         ch_free(c->value_string);
3069         return(ldap_pvt_tls_set_option(ld, flag, c->argv[1]));
3070 }
3071
3072 /* FIXME: this ought to be provided by libldap */
3073 static int
3074 config_tls_config(ConfigArgs *c) {
3075         int i, flag;
3076         switch(c->type) {
3077         case CFG_TLS_CRLCHECK:  flag = LDAP_OPT_X_TLS_CRLCHECK; break;
3078         case CFG_TLS_VERIFY:    flag = LDAP_OPT_X_TLS_REQUIRE_CERT; break;
3079         default:
3080                 Debug(LDAP_DEBUG_ANY, "%s: "
3081                                 "unknown tls_option <0x%x>\n",
3082                                 c->log, c->type, 0);
3083                 return 1;
3084         }
3085         if (c->op == SLAP_CONFIG_EMIT) {
3086                 return slap_tls_get_config( slap_tls_ld, flag, &c->value_string );
3087         } else if ( c->op == LDAP_MOD_DELETE ) {
3088                 int i = 0;
3089                 return ldap_pvt_tls_set_option( slap_tls_ld, flag, &i );
3090         }
3091         ch_free( c->value_string );
3092         if ( isdigit( (unsigned char)c->argv[1][0] ) ) {
3093                 if ( lutil_atoi( &i, c->argv[1] ) != 0 ) {
3094                         Debug(LDAP_DEBUG_ANY, "%s: "
3095                                 "unable to parse %s \"%s\"\n",
3096                                 c->log, c->argv[0], c->argv[1] );
3097                         return 1;
3098                 }
3099                 return(ldap_pvt_tls_set_option(slap_tls_ld, flag, &i));
3100         } else {
3101                 return(ldap_int_tls_config(slap_tls_ld, flag, c->argv[1]));
3102         }
3103 }
3104 #endif
3105
3106 static CfEntryInfo *
3107 config_find_base( CfEntryInfo *root, struct berval *dn, CfEntryInfo **last )
3108 {
3109         struct berval cdn;
3110         char *c;
3111
3112         if ( !root ) {
3113                 *last = NULL;
3114                 return NULL;
3115         }
3116
3117         if ( dn_match( &root->ce_entry->e_nname, dn ))
3118                 return root;
3119
3120         c = dn->bv_val+dn->bv_len;
3121         for (;*c != ',';c--);
3122
3123         while(root) {
3124                 *last = root;
3125                 for (--c;c>dn->bv_val && *c != ',';c--);
3126                 cdn.bv_val = c;
3127                 if ( *c == ',' )
3128                         cdn.bv_val++;
3129                 cdn.bv_len = dn->bv_len - (cdn.bv_val - dn->bv_val);
3130
3131                 root = root->ce_kids;
3132
3133                 for (;root;root=root->ce_sibs) {
3134                         if ( dn_match( &root->ce_entry->e_nname, &cdn )) {
3135                                 if ( cdn.bv_val == dn->bv_val ) {
3136                                         return root;
3137                                 }
3138                                 break;
3139                         }
3140                 }
3141         }
3142         return root;
3143 }
3144
3145 typedef struct setup_cookie {
3146         CfBackInfo *cfb;
3147         ConfigArgs *ca;
3148         Entry *frontend;
3149         Entry *config;
3150         int     got_frontend;
3151         int got_config;
3152 } setup_cookie;
3153
3154 static int
3155 config_ldif_resp( Operation *op, SlapReply *rs )
3156 {
3157         if ( rs->sr_type == REP_SEARCH ) {
3158                 setup_cookie *sc = op->o_callback->sc_private;
3159
3160                 sc->cfb->cb_got_ldif = 1;
3161                 /* Does the frontend exist? */
3162                 if ( !sc->got_frontend ) {
3163                         if ( !strncmp( rs->sr_entry->e_nname.bv_val,
3164                                 "olcDatabase", STRLENOF( "olcDatabase" ))) {
3165                                 if ( strncmp( rs->sr_entry->e_nname.bv_val +
3166                                         STRLENOF( "olcDatabase" ), "={-1}frontend",
3167                                         STRLENOF( "={-1}frontend" ))) {
3168                                         struct berval rdn;
3169                                         int i = op->o_noop;
3170                                         sc->ca->be = frontendDB;
3171                                         sc->ca->bi = frontendDB->bd_info;
3172                                         frontendDB->be_cf_ocs = &CFOC_FRONTEND;
3173                                         rdn.bv_val = sc->ca->log;
3174                                         rdn.bv_len = snprintf(rdn.bv_val, sizeof( sc->ca->log ),
3175                                                 "%s=" SLAP_X_ORDERED_FMT "%s",
3176                                                 cfAd_database->ad_cname.bv_val, -1,
3177                                                 sc->ca->bi->bi_type);
3178                                         op->o_noop = 1;
3179                                         sc->frontend = config_build_entry( op, rs,
3180                                                 sc->cfb->cb_root, sc->ca, &rdn, &CFOC_DATABASE,
3181                                                 sc->ca->be->be_cf_ocs );
3182                                         op->o_noop = i;
3183                                         sc->got_frontend++;
3184                                 } else {
3185                                         sc->got_frontend++;
3186                                         goto ok;
3187                                 }
3188                         }
3189                 }
3190                 /* Does the configDB exist? */
3191                 if ( sc->got_frontend && !sc->got_config &&
3192                         !strncmp( rs->sr_entry->e_nname.bv_val,
3193                         "olcDatabase", STRLENOF( "olcDatabase" ))) {
3194                         if ( strncmp( rs->sr_entry->e_nname.bv_val +
3195                                 STRLENOF( "olcDatabase" ), "={0}config",
3196                                 STRLENOF( "={0}config" ))) {
3197                                 struct berval rdn;
3198                                 int i = op->o_noop;
3199                                 sc->ca->be = LDAP_STAILQ_FIRST( &backendDB );
3200                                 sc->ca->bi = sc->ca->be->bd_info;
3201                                 rdn.bv_val = sc->ca->log;
3202                                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( sc->ca->log ),
3203                                         "%s=" SLAP_X_ORDERED_FMT "%s",
3204                                         cfAd_database->ad_cname.bv_val, 0,
3205                                         sc->ca->bi->bi_type);
3206                                 op->o_noop = 1;
3207                                 sc->config = config_build_entry( op, rs, sc->cfb->cb_root,
3208                                         sc->ca, &rdn, &CFOC_DATABASE, sc->ca->be->be_cf_ocs );
3209                                 op->o_noop = i;
3210                         }
3211                         sc->got_config++;
3212                 }
3213
3214 ok:
3215                 rs->sr_err = config_add_internal( sc->cfb, rs->sr_entry, sc->ca, NULL, NULL, NULL );
3216                 if ( rs->sr_err != LDAP_SUCCESS ) {
3217                         Debug( LDAP_DEBUG_ANY, "config error processing %s: %s\n",
3218                                 rs->sr_entry->e_name.bv_val, sc->ca->cr_msg, 0 );
3219                 }
3220         }
3221         return rs->sr_err;
3222 }
3223
3224 /* Configure and read the underlying back-ldif store */
3225 static int
3226 config_setup_ldif( BackendDB *be, const char *dir, int readit ) {
3227         CfBackInfo *cfb = be->be_private;
3228         ConfigArgs c = {0};
3229         ConfigTable *ct;
3230         char *argv[3];
3231         int rc = 0;
3232         setup_cookie sc;
3233         slap_callback cb = { NULL, config_ldif_resp, NULL, NULL };
3234         Connection conn = {0};
3235         OperationBuffer opbuf;
3236         Operation *op;
3237         SlapReply rs = {REP_RESULT};
3238         Filter filter = { LDAP_FILTER_PRESENT };
3239         struct berval filterstr = BER_BVC("(objectclass=*)");
3240         struct stat st;
3241
3242         /* Is the config directory available? */
3243         if ( stat( dir, &st ) < 0 ) {
3244                 /* No, so don't bother using the backing store.
3245                  * All changes will be in-memory only.
3246                  */
3247                 return 0;
3248         }
3249                 
3250         cfb->cb_db.bd_info = backend_info( "ldif" );
3251         if ( !cfb->cb_db.bd_info )
3252                 return 0;       /* FIXME: eventually this will be a fatal error */
3253
3254         if ( backend_db_init( "ldif", &cfb->cb_db, -1, NULL ) == NULL )
3255                 return 1;
3256
3257         cfb->cb_db.be_suffix = be->be_suffix;
3258         cfb->cb_db.be_nsuffix = be->be_nsuffix;
3259
3260         /* The suffix is always "cn=config". The underlying DB's rootdn
3261          * is always the same as the suffix.
3262          */
3263         cfb->cb_db.be_rootdn = be->be_suffix[0];
3264         cfb->cb_db.be_rootndn = be->be_nsuffix[0];
3265
3266         ber_str2bv( dir, 0, 1, &cfdir );
3267
3268         c.be = &cfb->cb_db;
3269         c.fname = "slapd";
3270         c.argc = 2;
3271         argv[0] = "directory";
3272         argv[1] = (char *)dir;
3273         argv[2] = NULL;
3274         c.argv = argv;
3275         c.reply.err = 0;
3276         c.reply.msg[0] = 0;
3277         c.table = Cft_Database;
3278
3279         ct = config_find_keyword( c.be->be_cf_ocs->co_table, &c );
3280         if ( !ct )
3281                 return 1;
3282
3283         if ( config_add_vals( ct, &c ))
3284                 return 1;
3285
3286         if ( backend_startup_one( &cfb->cb_db, &c.reply ))
3287                 return 1;
3288
3289         if ( readit ) {
3290                 void *thrctx = ldap_pvt_thread_pool_context();
3291                 int prev_DN_strict;
3292
3293                 connection_fake_init( &conn, &opbuf, thrctx );
3294                 op = &opbuf.ob_op;
3295
3296                 filter.f_desc = slap_schema.si_ad_objectClass;
3297
3298                 op->o_tag = LDAP_REQ_SEARCH;
3299
3300                 op->ors_filter = &filter;
3301                 op->ors_filterstr = filterstr;
3302                 op->ors_scope = LDAP_SCOPE_SUBTREE;
3303
3304                 op->o_dn = c.be->be_rootdn;
3305                 op->o_ndn = c.be->be_rootndn;
3306
3307                 op->o_req_dn = be->be_suffix[0];
3308                 op->o_req_ndn = be->be_nsuffix[0];
3309
3310                 op->ors_tlimit = SLAP_NO_LIMIT;
3311                 op->ors_slimit = SLAP_NO_LIMIT;
3312
3313                 op->ors_attrs = slap_anlist_all_attributes;
3314                 op->ors_attrsonly = 0;
3315
3316                 op->o_callback = &cb;
3317                 sc.cfb = cfb;
3318                 sc.ca = &c;
3319                 cb.sc_private = &sc;
3320                 sc.got_frontend = 0;
3321                 sc.got_config = 0;
3322                 sc.frontend = NULL;
3323                 sc.config = NULL;
3324
3325                 op->o_bd = &cfb->cb_db;
3326                 
3327                 /* Allow unknown attrs in DNs */
3328                 prev_DN_strict = slap_DN_strict;
3329                 slap_DN_strict = 0;
3330
3331                 rc = op->o_bd->be_search( op, &rs );
3332
3333                 /* Restore normal DN validation */
3334                 slap_DN_strict = prev_DN_strict;
3335
3336                 op->o_tag = LDAP_REQ_ADD;
3337                 if ( rc == LDAP_SUCCESS && sc.frontend ) {
3338                         op->ora_e = sc.frontend;
3339                         rc = op->o_bd->be_add( op, &rs );
3340                 }
3341                 if ( rc == LDAP_SUCCESS && sc.config ) {
3342                         op->ora_e = sc.config;
3343                         rc = op->o_bd->be_add( op, &rs );
3344                 }
3345                 ldap_pvt_thread_pool_context_reset( thrctx );
3346         }
3347
3348         /* ITS#4194 - only use if it's present, or we're converting. */
3349         if ( !readit || rc == LDAP_SUCCESS )
3350                 cfb->cb_use_ldif = 1;
3351
3352         return rc;
3353 }
3354
3355 static int
3356 CfOc_cmp( const void *c1, const void *c2 ) {
3357         const ConfigOCs *co1 = c1;
3358         const ConfigOCs *co2 = c2;
3359
3360         return ber_bvcmp( co1->co_name, co2->co_name );
3361 }
3362
3363 int
3364 config_register_schema(ConfigTable *ct, ConfigOCs *ocs) {
3365         int i;
3366
3367         i = init_config_attrs( ct );
3368         if ( i ) return i;
3369
3370         /* set up the objectclasses */
3371         i = init_config_ocs( ocs );
3372         if ( i ) return i;
3373
3374         for (i=0; ocs[i].co_def; i++) {
3375                 if ( ocs[i].co_oc ) {
3376                         ocs[i].co_name = &ocs[i].co_oc->soc_cname;
3377                         if ( !ocs[i].co_table )
3378                                 ocs[i].co_table = ct;
3379                         avl_insert( &CfOcTree, &ocs[i], CfOc_cmp, avl_dup_error );
3380                 }
3381         }
3382         return 0;
3383 }
3384
3385 int
3386 read_config(const char *fname, const char *dir) {
3387         BackendDB *be;
3388         CfBackInfo *cfb;
3389         const char *cfdir, *cfname;
3390         int rc;
3391
3392         /* Setup the config backend */
3393         be = backend_db_init( "config", NULL, 0, NULL );
3394         if ( !be )
3395                 return 1;
3396
3397         cfb = be->be_private;
3398         be->be_dfltaccess = ACL_NONE;
3399
3400         /* If no .conf, or a dir was specified, setup the dir */
3401         if ( !fname || dir ) {
3402                 if ( dir ) {
3403                         /* If explicitly given, check for existence */
3404                         struct stat st;
3405
3406                         if ( stat( dir, &st ) < 0 ) {
3407                                 Debug( LDAP_DEBUG_ANY,
3408                                         "invalid config directory %s, error %d\n",
3409                                                 dir, errno, 0 );
3410                                 return 1;
3411                         }
3412                         cfdir = dir;
3413                 } else {
3414                         cfdir = SLAPD_DEFAULT_CONFIGDIR;
3415                 }
3416                 /* if fname is defaulted, try reading .d */
3417                 rc = config_setup_ldif( be, cfdir, !fname );
3418
3419                 if ( rc ) {
3420                         /* It may be OK if the base object doesn't exist yet. */
3421                         if ( rc != LDAP_NO_SUCH_OBJECT )
3422                                 return 1;
3423                         /* ITS#4194: But if dir was specified and no fname,
3424                          * then we were supposed to read the dir. Unless we're
3425                          * trying to slapadd the dir...
3426                          */
3427                         if ( dir && !fname ) {
3428                                 if ( slapMode & (SLAP_SERVER_MODE|SLAP_TOOL_READMAIN|SLAP_TOOL_READONLY))
3429                                         return 1;
3430                                 /* Assume it's slapadd with a config dir, let it continue */
3431                                 rc = 0;
3432                                 cfb->cb_got_ldif = 1;
3433                                 cfb->cb_use_ldif = 1;
3434                                 goto done;
3435                         }
3436                 }
3437
3438                 /* If we read the config from back-ldif, nothing to do here */
3439                 if ( cfb->cb_got_ldif ) {
3440                         rc = 0;
3441                         goto done;
3442                 }
3443         }
3444
3445         if ( fname )
3446                 cfname = fname;
3447         else
3448                 cfname = SLAPD_DEFAULT_CONFIGFILE;
3449
3450         rc = read_config_file(cfname, 0, NULL, config_back_cf_table);
3451
3452         if ( rc == 0 )
3453                 ber_str2bv( cfname, 0, 1, &cfb->cb_config->c_file );
3454
3455 done:
3456         if ( rc == 0 && BER_BVISNULL( &frontendDB->be_schemadn ) ) {
3457                 ber_str2bv( SLAPD_SCHEMA_DN, STRLENOF( SLAPD_SCHEMA_DN ), 1,
3458                         &frontendDB->be_schemadn );
3459                 rc = dnNormalize( 0, NULL, NULL, &frontendDB->be_schemadn, &frontendDB->be_schemandn, NULL );
3460                 if ( rc != LDAP_SUCCESS ) {
3461                         Debug(LDAP_DEBUG_ANY, "read_config: "
3462                                 "unable to normalize default schema DN \"%s\"\n",
3463                                 frontendDB->be_schemadn.bv_val, 0, 0 );
3464                         /* must not happen */
3465                         assert( 0 );
3466                 }
3467         }
3468         return rc;
3469 }
3470
3471 static int
3472 config_back_bind( Operation *op, SlapReply *rs )
3473 {
3474         if ( be_isroot_pw( op ) ) {
3475                 ber_dupbv( &op->orb_edn, be_root_dn( op->o_bd ));
3476                 /* frontend sends result */
3477                 return LDAP_SUCCESS;
3478         }
3479
3480         rs->sr_err = LDAP_INVALID_CREDENTIALS;
3481         send_ldap_result( op, rs );
3482
3483         return rs->sr_err;
3484 }
3485
3486 static int
3487 config_send( Operation *op, SlapReply *rs, CfEntryInfo *ce, int depth )
3488 {
3489         int rc = 0;
3490
3491         if ( test_filter( op, ce->ce_entry, op->ors_filter ) == LDAP_COMPARE_TRUE )
3492         {
3493                 rs->sr_attrs = op->ors_attrs;
3494                 rs->sr_entry = ce->ce_entry;
3495                 rs->sr_flags = 0;
3496                 rc = send_search_entry( op, rs );
3497         }
3498         if ( op->ors_scope == LDAP_SCOPE_SUBTREE ) {
3499                 if ( ce->ce_kids ) {
3500                         rc = config_send( op, rs, ce->ce_kids, 1 );
3501                         if ( rc ) return rc;
3502                 }
3503                 if ( depth ) {
3504                         for (ce=ce->ce_sibs; ce; ce=ce->ce_sibs) {
3505                                 rc = config_send( op, rs, ce, 0 );
3506                                 if ( rc ) break;
3507                         }
3508                 }
3509         }
3510         return rc;
3511 }
3512
3513 static ConfigTable *
3514 config_find_table( ConfigOCs **colst, int nocs, AttributeDescription *ad,
3515         ConfigArgs *ca )
3516 {
3517         int i, j;
3518
3519         for (j=0; j<nocs; j++) {
3520                 for (i=0; colst[j]->co_table[i].name; i++)
3521                         if ( colst[j]->co_table[i].ad == ad ) {
3522                                 ca->table = colst[j]->co_type;
3523                                 return &colst[j]->co_table[i];
3524                         }
3525         }
3526         return NULL;
3527 }
3528
3529 /* Sort the attributes of the entry according to the order defined
3530  * in the objectclass, with required attributes occurring before
3531  * allowed attributes. For any attributes with sequencing dependencies
3532  * (e.g., rootDN must be defined after suffix) the objectclass must
3533  * list the attributes in the desired sequence.
3534  */
3535 static void
3536 sort_attrs( Entry *e, ConfigOCs **colst, int nocs )
3537 {
3538         Attribute *a, *head = NULL, *tail = NULL, **prev;
3539         int i, j;
3540
3541         for (i=0; i<nocs; i++) {
3542                 if ( colst[i]->co_oc->soc_required ) {
3543                         AttributeType **at = colst[i]->co_oc->soc_required;
3544                         for (j=0; at[j]; j++) {
3545                                 for (a=e->e_attrs, prev=&e->e_attrs; a;
3546                                         prev = &(*prev)->a_next, a=a->a_next) {
3547                                         if ( a->a_desc == at[j]->sat_ad ) {
3548                                                 *prev = a->a_next;
3549                                                 if (!head) {
3550                                                         head = a;
3551                                                         tail = a;
3552                                                 } else {
3553                                                         tail->a_next = a;
3554                                                         tail = a;
3555                                                 }
3556                                                 break;
3557                                         }
3558                                 }
3559                         }
3560                 }
3561                 if ( colst[i]->co_oc->soc_allowed ) {
3562                         AttributeType **at = colst[i]->co_oc->soc_allowed;
3563                         for (j=0; at[j]; j++) {
3564                                 for (a=e->e_attrs, prev=&e->e_attrs; a;
3565                                         prev = &(*prev)->a_next, a=a->a_next) {
3566                                         if ( a->a_desc == at[j]->sat_ad ) {
3567                                                 *prev = a->a_next;
3568                                                 if (!head) {
3569                                                         head = a;
3570                                                         tail = a;
3571                                                 } else {
3572                                                         tail->a_next = a;
3573                                                         tail = a;
3574                                                 }
3575                                                 break;
3576                                         }
3577                                 }
3578                         }
3579                 }
3580         }
3581         if ( tail ) {
3582                 tail->a_next = e->e_attrs;
3583                 e->e_attrs = head;
3584         }
3585 }
3586
3587 static int
3588 check_vals( ConfigTable *ct, ConfigArgs *ca, void *ptr, int isAttr )
3589 {
3590         Attribute *a = NULL;
3591         AttributeDescription *ad;
3592         BerVarray vals;
3593
3594         int i, rc = 0;
3595
3596         if ( isAttr ) {
3597                 a = ptr;
3598                 ad = a->a_desc;
3599                 vals = a->a_vals;
3600         } else {
3601                 Modifications *ml = ptr;
3602                 ad = ml->sml_desc;
3603                 vals = ml->sml_values;
3604         }
3605
3606         if ( a && ( ad->ad_type->sat_flags & SLAP_AT_ORDERED_VAL )) {
3607                 rc = ordered_value_sort( a, 1 );
3608                 if ( rc ) {
3609                         snprintf(ca->cr_msg, sizeof( ca->cr_msg ), "ordered_value_sort failed on attr %s\n",
3610                                 ad->ad_cname.bv_val );
3611                         return rc;
3612                 }
3613         }
3614         for ( i=0; vals[i].bv_val; i++ ) {
3615                 ca->line = vals[i].bv_val;
3616                 if (( ad->ad_type->sat_flags & SLAP_AT_ORDERED_VAL ) &&
3617                         ca->line[0] == '{' ) {
3618                         char *idx = strchr( ca->line, '}' );
3619                         if ( idx ) ca->line = idx+1;
3620                 }
3621                 rc = config_parse_vals( ct, ca, i );
3622                 if ( rc ) {
3623                         break;
3624                 }
3625         }
3626         return rc;
3627 }
3628
3629 static int
3630 config_rename_attr( SlapReply *rs, Entry *e, struct berval *rdn,
3631         Attribute **at )
3632 {
3633         struct berval rtype, rval;
3634         Attribute *a;
3635         AttributeDescription *ad = NULL;
3636
3637         dnRdn( &e->e_name, rdn );
3638         rval.bv_val = strchr(rdn->bv_val, '=' ) + 1;
3639         rval.bv_len = rdn->bv_len - (rval.bv_val - rdn->bv_val);
3640         rtype.bv_val = rdn->bv_val;
3641         rtype.bv_len = rval.bv_val - rtype.bv_val - 1;
3642
3643         /* Find attr */
3644         slap_bv2ad( &rtype, &ad, &rs->sr_text );
3645         a = attr_find( e->e_attrs, ad );
3646         if (!a ) return LDAP_NAMING_VIOLATION;
3647         *at = a;
3648
3649         return 0;
3650 }
3651
3652 static void
3653 config_rename_kids( CfEntryInfo *ce )
3654 {
3655         CfEntryInfo *ce2;
3656         struct berval rdn, nrdn;
3657
3658         for (ce2 = ce->ce_kids; ce2; ce2 = ce2->ce_sibs) {
3659                 dnRdn ( &ce2->ce_entry->e_name, &rdn );
3660                 dnRdn ( &ce2->ce_entry->e_nname, &nrdn );
3661                 free( ce2->ce_entry->e_name.bv_val );
3662                 free( ce2->ce_entry->e_nname.bv_val );
3663                 build_new_dn( &ce2->ce_entry->e_name, &ce->ce_entry->e_name,
3664                         &rdn, NULL );
3665                 build_new_dn( &ce2->ce_entry->e_nname, &ce->ce_entry->e_nname,
3666                         &nrdn, NULL );
3667                 config_rename_kids( ce2 );
3668         }
3669 }
3670
3671 static int
3672 config_rename_one( Operation *op, SlapReply *rs, Entry *e,
3673         CfEntryInfo *parent, Attribute *a, struct berval *newrdn,
3674         struct berval *nnewrdn, int use_ldif )
3675 {
3676         char *ptr1;
3677         int rc = 0;
3678         struct berval odn, ondn;
3679
3680         odn = e->e_name;
3681         ondn = e->e_nname;
3682         build_new_dn( &e->e_name, &parent->ce_entry->e_name, newrdn, NULL );
3683         build_new_dn( &e->e_nname, &parent->ce_entry->e_nname, nnewrdn, NULL );
3684
3685         /* Replace attr */
3686         free( a->a_vals[0].bv_val );
3687         ptr1 = strchr( newrdn->bv_val, '=' ) + 1;
3688         a->a_vals[0].bv_len = newrdn->bv_len - (ptr1 - newrdn->bv_val);
3689         a->a_vals[0].bv_val = ch_malloc( a->a_vals[0].bv_len + 1 );
3690         strcpy( a->a_vals[0].bv_val, ptr1 );
3691
3692         if ( a->a_nvals != a->a_vals ) {
3693                 free( a->a_nvals[0].bv_val );
3694                 ptr1 = strchr( nnewrdn->bv_val, '=' ) + 1;
3695                 a->a_nvals[0].bv_len = nnewrdn->bv_len - (ptr1 - nnewrdn->bv_val);
3696                 a->a_nvals[0].bv_val = ch_malloc( a->a_nvals[0].bv_len + 1 );
3697                 strcpy( a->a_nvals[0].bv_val, ptr1 );
3698         }
3699         if ( use_ldif ) {
3700                 CfBackInfo *cfb = (CfBackInfo *)op->o_bd->be_private;
3701                 BackendDB *be = op->o_bd;
3702                 slap_callback sc = { NULL, slap_null_cb, NULL, NULL }, *scp;
3703                 struct berval dn, ndn, xdn, xndn;
3704
3705                 op->o_bd = &cfb->cb_db;
3706
3707                 /* Save current rootdn; use the underlying DB's rootdn */
3708                 dn = op->o_dn;
3709                 ndn = op->o_ndn;
3710                 xdn = op->o_req_dn;
3711                 xndn = op->o_req_ndn;
3712                 op->o_dn = op->o_bd->be_rootdn;
3713                 op->o_ndn = op->o_bd->be_rootndn;
3714                 op->o_req_dn = odn;
3715                 op->o_req_ndn = ondn;
3716
3717                 scp = op->o_callback;
3718                 op->o_callback = &sc;
3719                 op->orr_newrdn = *newrdn;
3720                 op->orr_nnewrdn = *nnewrdn;
3721                 op->orr_newSup = NULL;
3722                 op->orr_nnewSup = NULL;
3723                 op->orr_deleteoldrdn = 1;
3724                 op->orr_modlist = NULL;
3725                 slap_modrdn2mods( op, rs );
3726                 slap_mods_opattrs( op, &op->orr_modlist, 1 );
3727                 rc = op->o_bd->be_modrdn( op, rs );
3728                 slap_mods_free( op->orr_modlist, 1 );
3729
3730                 op->o_bd = be;
3731                 op->o_callback = scp;
3732                 op->o_dn = dn;
3733                 op->o_ndn = ndn;
3734                 op->o_req_dn = xdn;
3735                 op->o_req_ndn = xndn;
3736         }
3737         free( odn.bv_val );
3738         free( ondn.bv_val );
3739         if ( e->e_private )
3740                 config_rename_kids( e->e_private );
3741         return rc;
3742 }
3743
3744 static int
3745 config_renumber_one( Operation *op, SlapReply *rs, CfEntryInfo *parent, 
3746         Entry *e, int idx, int tailindex, int use_ldif )
3747 {
3748         struct berval ival, newrdn, nnewrdn;
3749         struct berval rdn;
3750         Attribute *a;
3751         char ibuf[32], *ptr1, *ptr2 = NULL;
3752         int rc = 0;
3753
3754         rc = config_rename_attr( rs, e, &rdn, &a );
3755         if ( rc ) return rc;
3756
3757         ival.bv_val = ibuf;
3758         ival.bv_len = snprintf( ibuf, sizeof( ibuf ), SLAP_X_ORDERED_FMT, idx );
3759         if ( ival.bv_len >= sizeof( ibuf ) ) {
3760                 return LDAP_NAMING_VIOLATION;
3761         }
3762         
3763         newrdn.bv_len = rdn.bv_len + ival.bv_len;
3764         newrdn.bv_val = ch_malloc( newrdn.bv_len+1 );
3765
3766         if ( tailindex ) {
3767                 ptr1 = lutil_strncopy( newrdn.bv_val, rdn.bv_val, rdn.bv_len );
3768                 ptr1 = lutil_strcopy( ptr1, ival.bv_val );
3769         } else {
3770                 int xlen;
3771                 ptr2 = ber_bvchr( &rdn, '}' );
3772                 if ( ptr2 ) {
3773                         ptr2++;
3774                 } else {
3775                         ptr2 = rdn.bv_val + a->a_desc->ad_cname.bv_len + 1;
3776                 }
3777                 xlen = rdn.bv_len - (ptr2 - rdn.bv_val);
3778                 ptr1 = lutil_strncopy( newrdn.bv_val, a->a_desc->ad_cname.bv_val,
3779                         a->a_desc->ad_cname.bv_len );
3780                 *ptr1++ = '=';
3781                 ptr1 = lutil_strcopy( ptr1, ival.bv_val );
3782                 ptr1 = lutil_strncopy( ptr1, ptr2, xlen );
3783                 *ptr1 = '\0';
3784         }
3785
3786         /* Do the equivalent of ModRDN */
3787         /* Replace DN / NDN */
3788         newrdn.bv_len = ptr1 - newrdn.bv_val;
3789         rdnNormalize( 0, NULL, NULL, &newrdn, &nnewrdn, NULL );
3790         rc = config_rename_one( op, rs, e, parent, a, &newrdn, &nnewrdn, use_ldif );
3791
3792         free( nnewrdn.bv_val );
3793         free( newrdn.bv_val );
3794         return rc;
3795 }
3796
3797 static int
3798 check_name_index( CfEntryInfo *parent, ConfigType ce_type, Entry *e,
3799         SlapReply *rs, int *renum, int *ibase )
3800 {
3801         CfEntryInfo *ce;
3802         int index = -1, gotindex = 0, nsibs, rc = 0;
3803         int renumber = 0, tailindex = 0, isfrontend = 0, isconfig = 0;
3804         char *ptr1, *ptr2 = NULL;
3805         struct berval rdn;
3806
3807         if ( renum ) *renum = 0;
3808
3809         /* These entries don't get indexed/renumbered */
3810         if ( ce_type == Cft_Global ) return 0;
3811         if ( ce_type == Cft_Schema && parent->ce_type == Cft_Global ) return 0;
3812
3813         if ( ce_type == Cft_Module )
3814                 tailindex = 1;
3815
3816         /* See if the rdn has an index already */
3817         dnRdn( &e->e_name, &rdn );
3818         if ( ce_type == Cft_Database ) {
3819                 if ( !strncmp( rdn.bv_val + rdn.bv_len - STRLENOF("frontend"),
3820                                 "frontend", STRLENOF("frontend") )) 
3821                         isfrontend = 1;
3822                 else if ( !strncmp( rdn.bv_val + rdn.bv_len - STRLENOF("config"),
3823                                 "config", STRLENOF("config") )) 
3824                         isconfig = 1;
3825         }
3826         ptr1 = ber_bvchr( &e->e_name, '{' );
3827         if ( ptr1 && ptr1 - e->e_name.bv_val < rdn.bv_len ) {
3828                 char    *next;
3829                 ptr2 = strchr( ptr1, '}' );
3830                 if (!ptr2 || ptr2 - e->e_name.bv_val > rdn.bv_len)
3831                         return LDAP_NAMING_VIOLATION;
3832                 if ( ptr2-ptr1 == 1)
3833                         return LDAP_NAMING_VIOLATION;
3834                 gotindex = 1;
3835                 index = strtol( ptr1 + 1, &next, 10 );
3836                 if ( next == ptr1 + 1 || next[ 0 ] != '}' ) {
3837                         return LDAP_NAMING_VIOLATION;
3838                 }
3839                 if ( index < 0 ) {
3840                         /* Special case, we allow -1 for the frontendDB */
3841                         if ( index != -1 || !isfrontend )
3842                                 return LDAP_NAMING_VIOLATION;
3843                 }
3844                 if ( isconfig && index != 0 ){
3845                         return LDAP_NAMING_VIOLATION;
3846                 }
3847         }
3848
3849         /* count related kids */
3850         for (nsibs=0, ce=parent->ce_kids; ce; ce=ce->ce_sibs) {
3851                 if ( ce->ce_type == ce_type ) nsibs++;
3852         }
3853
3854         /* account for -1 frontend */
3855         if ( ce_type == Cft_Database )
3856                 nsibs--;
3857
3858         if ( index != nsibs ) {
3859                 if ( gotindex ) {
3860                         if ( index < nsibs ) {
3861                                 if ( tailindex ) return LDAP_NAMING_VIOLATION;
3862                                 /* Siblings need to be renumbered */
3863                                 if ( index != -1 || !isfrontend )
3864                                         renumber = 1;
3865                         }
3866                 }
3867                 /* config DB is always "0" */
3868                 if ( isconfig && index == -1 ) {
3869                         index = 0;
3870                 }
3871                 if ( !isfrontend && index == -1 ) {
3872                         index = nsibs;
3873                 }
3874
3875                 /* just make index = nsibs */
3876                 if ( !renumber ) {
3877                         rc = config_renumber_one( NULL, rs, parent, e, index, tailindex, 0 );
3878                 }
3879         }
3880         if ( ibase ) *ibase = index;
3881         if ( renum ) *renum = renumber;
3882         return rc;
3883 }
3884
3885 static int
3886 count_oc( ObjectClass *oc, ConfigOCs ***copp, int *nocs )
3887 {
3888         ConfigOCs       co, *cop;
3889         ObjectClass     **sups;
3890
3891         co.co_name = &oc->soc_cname;
3892         cop = avl_find( CfOcTree, &co, CfOc_cmp );
3893         if ( cop ) {
3894                 int     i;
3895
3896                 /* check for duplicates */
3897                 for ( i = 0; i < *nocs; i++ ) {
3898                         if ( *copp && (*copp)[i] == cop ) {
3899                                 break;
3900                         }
3901                 }
3902
3903                 if ( i == *nocs ) {
3904                         ConfigOCs **tmp = ch_realloc( *copp, (*nocs + 1)*sizeof( ConfigOCs * ) );
3905                         if ( tmp == NULL ) {
3906                                 return -1;
3907                         }
3908                         *copp = tmp;
3909                         (*copp)[*nocs] = cop;
3910                         (*nocs)++;
3911                 }
3912         }
3913
3914         for ( sups = oc->soc_sups; sups && *sups; sups++ ) {
3915                 if ( count_oc( *sups, copp, nocs ) ) {
3916                         return -1;
3917                 }
3918         }
3919
3920         return 0;
3921 }
3922
3923 static ConfigOCs **
3924 count_ocs( Attribute *oc_at, int *nocs )
3925 {
3926         int             i;
3927         ConfigOCs       **colst = NULL;
3928
3929         *nocs = 0;
3930
3931         for ( i = 0; !BER_BVISNULL( &oc_at->a_nvals[i] ); i++ )
3932                 /* count attrs */ ;
3933
3934         for ( ; i--; ) {
3935                 ObjectClass     *oc = oc_bvfind( &oc_at->a_nvals[i] );
3936
3937                 assert( oc != NULL );
3938                 if ( count_oc( oc, &colst, nocs ) ) {
3939                         ch_free( colst );
3940                         return NULL;
3941                 }
3942         }
3943
3944         return colst;
3945 }
3946
3947 static int
3948 cfAddInclude( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
3949 {
3950         /* Leftover from RE23. Never parse this entry */
3951         return LDAP_COMPARE_TRUE;
3952 }
3953
3954 static int
3955 cfAddSchema( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
3956 {
3957         ConfigFile *cfo;
3958
3959         /* This entry is hardcoded, don't re-parse it */
3960         if ( p->ce_type == Cft_Global ) {
3961                 cfn = p->ce_private;
3962                 ca->private = cfn;
3963                 return LDAP_COMPARE_TRUE;
3964         }
3965         if ( p->ce_type != Cft_Schema )
3966                 return LDAP_CONSTRAINT_VIOLATION;
3967
3968         cfn = ch_calloc( 1, sizeof(ConfigFile) );
3969         ca->private = cfn;
3970         cfo = p->ce_private;
3971         cfn->c_sibs = cfo->c_kids;
3972         cfo->c_kids = cfn;
3973         return LDAP_SUCCESS;
3974 }
3975
3976 static int
3977 cfAddDatabase( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
3978 {
3979         if ( p->ce_type != Cft_Global ) {
3980                 return LDAP_CONSTRAINT_VIOLATION;
3981         }
3982         ca->be = frontendDB;    /* just to get past check_vals */
3983         return LDAP_SUCCESS;
3984 }
3985
3986 static int
3987 cfAddBackend( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
3988 {
3989         if ( p->ce_type != Cft_Global ) {
3990                 return LDAP_CONSTRAINT_VIOLATION;
3991         }
3992         return LDAP_SUCCESS;
3993 }
3994
3995 static int
3996 cfAddModule( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
3997 {
3998         if ( p->ce_type != Cft_Global ) {
3999                 return LDAP_CONSTRAINT_VIOLATION;
4000         }
4001         return LDAP_SUCCESS;
4002 }
4003
4004 static int
4005 cfAddOverlay( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
4006 {
4007         if ( p->ce_type != Cft_Database ) {
4008                 return LDAP_CONSTRAINT_VIOLATION;
4009         }
4010         ca->be = p->ce_be;
4011         return LDAP_SUCCESS;
4012 }
4013
4014 static void
4015 schema_destroy_one( ConfigArgs *ca, ConfigOCs **colst, int nocs,
4016         CfEntryInfo *p )
4017 {
4018         ConfigTable *ct;
4019         ConfigFile *cfo;
4020         AttributeDescription *ad;
4021         const char *text;
4022
4023         ca->valx = -1;
4024         ca->line = NULL;
4025         if ( cfn->c_cr_head ) {
4026                 struct berval bv = BER_BVC("olcDitContentRules");
4027                 ad = NULL;
4028                 slap_bv2ad( &bv, &ad, &text );
4029                 ct = config_find_table( colst, nocs, ad, ca );
4030                 config_del_vals( ct, ca );
4031         }
4032         if ( cfn->c_oc_head ) {
4033                 struct berval bv = BER_BVC("olcObjectClasses");
4034                 ad = NULL;
4035                 slap_bv2ad( &bv, &ad, &text );
4036                 ct = config_find_table( colst, nocs, ad, ca );
4037                 config_del_vals( ct, ca );
4038         }
4039         if ( cfn->c_at_head ) {
4040                 struct berval bv = BER_BVC("olcAttributeTypes");
4041                 ad = NULL;
4042                 slap_bv2ad( &bv, &ad, &text );
4043                 ct = config_find_table( colst, nocs, ad, ca );
4044                 config_del_vals( ct, ca );
4045         }
4046         if ( cfn->c_om_head ) {
4047                 struct berval bv = BER_BVC("olcObjectIdentifier");
4048                 ad = NULL;
4049                 slap_bv2ad( &bv, &ad, &text );
4050                 ct = config_find_table( colst, nocs, ad, ca );
4051                 config_del_vals( ct, ca );
4052         }
4053         cfo = p->ce_private;
4054         cfo->c_kids = cfn->c_sibs;
4055         ch_free( cfn );
4056 }
4057
4058 static int
4059 config_add_oc( ConfigOCs **cop, CfEntryInfo *last, Entry *e, ConfigArgs *ca )
4060 {
4061         int             rc = LDAP_CONSTRAINT_VIOLATION;
4062         ObjectClass     **ocp;
4063
4064         if ( (*cop)->co_ldadd ) {
4065                 rc = (*cop)->co_ldadd( last, e, ca );
4066                 if ( rc != LDAP_CONSTRAINT_VIOLATION ) {
4067                         return rc;
4068                 }
4069         }
4070
4071         for ( ocp = (*cop)->co_oc->soc_sups; ocp && *ocp; ocp++ ) {
4072                 ConfigOCs       co = { 0 };
4073
4074                 co.co_name = &(*ocp)->soc_cname;
4075                 *cop = avl_find( CfOcTree, &co, CfOc_cmp );
4076                 if ( *cop == NULL ) {
4077                         return rc;
4078                 }
4079
4080                 rc = config_add_oc( cop, last, e, ca );
4081                 if ( rc != LDAP_CONSTRAINT_VIOLATION ) {
4082                         return rc;
4083                 }
4084         }
4085
4086         return rc;
4087 }
4088
4089 /* Parse an LDAP entry into config directives */
4090 static int
4091 config_add_internal( CfBackInfo *cfb, Entry *e, ConfigArgs *ca, SlapReply *rs,
4092         int *renum, Operation *op )
4093 {
4094         CfEntryInfo     *ce, *last = NULL;
4095         ConfigOCs       co, *coptr, **colst;
4096         Attribute       *a, *oc_at, *soc_at;
4097         int             i, ibase = -1, nocs, rc = 0;
4098         struct berval   pdn;
4099         ConfigTable     *ct;
4100         char            *ptr, *log_prefix = op ? op->o_log_prefix : "";
4101
4102         memset( ca, 0, sizeof(ConfigArgs));
4103
4104         /* Make sure parent exists and entry does not. But allow
4105          * Databases and Overlays to be inserted. Don't do any
4106          * auto-renumbering if manageDSAit control is present.
4107          */
4108         ce = config_find_base( cfb->cb_root, &e->e_nname, &last );
4109         if ( ce ) {
4110                 if ( ( op && op->o_managedsait ) ||
4111                         ( ce->ce_type != Cft_Database && ce->ce_type != Cft_Overlay &&
4112                           ce->ce_type != Cft_Module ) )
4113                 {
4114                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4115                                 "DN=\"%s\" already exists\n",
4116                                 log_prefix, e->e_name.bv_val, 0 );
4117                         return LDAP_ALREADY_EXISTS;
4118                 }
4119         }
4120
4121         dnParent( &e->e_nname, &pdn );
4122
4123         /* If last is NULL, the new entry is the root/suffix entry, 
4124          * otherwise last should be the parent.
4125          */
4126         if ( last && !dn_match( &last->ce_entry->e_nname, &pdn ) ) {
4127                 if ( rs ) {
4128                         rs->sr_matched = last->ce_entry->e_name.bv_val;
4129                 }
4130                 Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4131                         "DN=\"%s\" not child of DN=\"%s\"\n",
4132                         log_prefix, e->e_name.bv_val,
4133                         last->ce_entry->e_name.bv_val );
4134                 return LDAP_NO_SUCH_OBJECT;
4135         }
4136
4137         if ( op ) {
4138                 /* No parent, must be root. This will never happen... */
4139                 if ( !last && !be_isroot( op ) && !be_shadow_update( op ) ) {
4140                         return LDAP_NO_SUCH_OBJECT;
4141                 }
4142
4143                 if ( last && !access_allowed( op, last->ce_entry,
4144                         slap_schema.si_ad_children, NULL, ACL_WADD, NULL ) )
4145                 {
4146                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4147                                 "DN=\"%s\" no write access to \"children\" of parent\n",
4148                                 log_prefix, e->e_name.bv_val, 0 );
4149                         return LDAP_INSUFFICIENT_ACCESS;
4150                 }
4151         }
4152
4153         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
4154         if ( !oc_at ) {
4155                 Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4156                         "DN=\"%s\" no objectClass\n",
4157                         log_prefix, e->e_name.bv_val, 0 );
4158                 return LDAP_OBJECT_CLASS_VIOLATION;
4159         }
4160
4161         soc_at = attr_find( e->e_attrs, slap_schema.si_ad_structuralObjectClass );
4162         if ( !soc_at ) {
4163                 ObjectClass     *soc = NULL;
4164                 char            textbuf[ SLAP_TEXT_BUFLEN ];
4165                 const char      *text = textbuf;
4166
4167                 /* FIXME: check result */
4168                 rc = structural_class( oc_at->a_nvals, &soc, NULL,
4169                         &text, textbuf, sizeof(textbuf), NULL );
4170                 if ( rc != LDAP_SUCCESS ) {
4171                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4172                                 "DN=\"%s\" no structural objectClass (%s)\n",
4173                                 log_prefix, e->e_name.bv_val, text );
4174                         return rc;
4175                 }
4176                 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass, &soc->soc_cname, NULL );
4177                 soc_at = attr_find( e->e_attrs, slap_schema.si_ad_structuralObjectClass );
4178                 if ( soc_at == NULL ) {
4179                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4180                                 "DN=\"%s\" no structural objectClass; "
4181                                 "unable to merge computed class %s\n",
4182                                 log_prefix, e->e_name.bv_val,
4183                                 soc->soc_cname.bv_val );
4184                         return LDAP_OBJECT_CLASS_VIOLATION;
4185                 }
4186
4187                 Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4188                         "DN=\"%s\" no structural objectClass; "
4189                         "computed objectClass %s merged\n",
4190                         log_prefix, e->e_name.bv_val,
4191                         soc->soc_cname.bv_val );
4192         }
4193
4194         /* Fake the coordinates based on whether we're part of an
4195          * LDAP Add or if reading the config dir
4196          */
4197         if ( rs ) {
4198                 ca->fname = "slapd";
4199                 ca->lineno = 0;
4200         } else {
4201                 ca->fname = cfdir.bv_val;
4202                 ca->lineno = 1;
4203         }
4204         ca->ca_op = op;
4205
4206         co.co_name = &soc_at->a_nvals[0];
4207         coptr = avl_find( CfOcTree, &co, CfOc_cmp );
4208         if ( coptr == NULL ) {
4209                 Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4210                         "DN=\"%s\" no structural objectClass in configuration table\n",
4211                         log_prefix, e->e_name.bv_val, 0 );
4212                 return LDAP_OBJECT_CLASS_VIOLATION;
4213         }
4214
4215         /* Only the root can be Cft_Global, everything else must
4216          * have a parent. Only limited nesting arrangements are allowed.
4217          */
4218         rc = LDAP_CONSTRAINT_VIOLATION;
4219         if ( coptr->co_type == Cft_Global && !last ) {
4220                 cfn = cfb->cb_config;
4221                 ca->private = cfn;
4222                 ca->be = frontendDB;    /* just to get past check_vals */
4223                 rc = LDAP_SUCCESS;
4224         }
4225
4226         colst = count_ocs( oc_at, &nocs );
4227
4228         /* Check whether the Add is allowed by its parent, and do
4229          * any necessary arg setup
4230          */
4231         if ( last ) {
4232                 rc = config_add_oc( &coptr, last, e, ca );
4233                 if ( rc == LDAP_CONSTRAINT_VIOLATION ) {
4234                         for ( i = 0; i<nocs; i++ ) {
4235                                 /* Already checked these */
4236                                 if ( colst[i]->co_oc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
4237                                         continue;
4238                                 if ( colst[i]->co_ldadd &&
4239                                         ( rc = colst[i]->co_ldadd( last, e, ca ))
4240                                                 != LDAP_CONSTRAINT_VIOLATION ) {
4241                                         coptr = colst[i];
4242                                         break;
4243                                 }
4244                         }
4245                 }
4246                 if ( rc == LDAP_CONSTRAINT_VIOLATION ) {
4247                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4248                                 "DN=\"%s\" no structural objectClass add function\n",
4249                                 log_prefix, e->e_name.bv_val, 0 );
4250                         return LDAP_OBJECT_CLASS_VIOLATION;
4251                 }
4252         }
4253
4254         /* Add the entry but don't parse it, we already have its contents */
4255         if ( rc == LDAP_COMPARE_TRUE ) {
4256                 rc = LDAP_SUCCESS;
4257                 goto ok;
4258         }
4259
4260         if ( rc != LDAP_SUCCESS )
4261                 goto done_noop;
4262
4263         /* Parse all the values and check for simple syntax errors before
4264          * performing any set actions.
4265          *
4266          * If doing an LDAPadd, check for indexed names and any necessary
4267          * renaming/renumbering. Entries that don't need indexed names are
4268          * ignored. Entries that need an indexed name and arrive without one
4269          * are assigned to the end. Entries that arrive with an index may
4270          * cause the following entries to be renumbered/bumped down.
4271          *
4272          * Note that "pseudo-indexed" entries (cn=Include{xx}, cn=Module{xx})
4273          * don't allow Adding an entry with an index that's already in use.
4274          * This is flagged as an error (LDAP_ALREADY_EXISTS) up above.
4275          *
4276          * These entries can have auto-assigned indexes (appended to the end)
4277          * but only the other types support auto-renumbering of siblings.
4278          */
4279         {
4280                 rc = check_name_index( last, coptr->co_type, e, rs, renum,
4281                         &ibase );
4282                 if ( rc ) {
4283                         goto done_noop;
4284                 }
4285                 if ( renum && *renum && coptr->co_type != Cft_Database &&
4286                         coptr->co_type != Cft_Overlay )
4287                 {
4288                         snprintf( ca->cr_msg, sizeof( ca->cr_msg ),
4289                                 "operation requires sibling renumbering" );
4290                         rc = LDAP_UNWILLING_TO_PERFORM;
4291                         goto done_noop;
4292                 }
4293         }
4294
4295         init_config_argv( ca );
4296
4297         /* Make sure we process attrs in the required order */
4298         sort_attrs( e, colst, nocs );
4299
4300         for ( a = e->e_attrs; a; a = a->a_next ) {
4301                 if ( a == oc_at ) continue;
4302                 ct = config_find_table( colst, nocs, a->a_desc, ca );
4303                 if ( !ct ) continue;    /* user data? */
4304                 rc = check_vals( ct, ca, a, 1 );
4305                 if ( rc ) goto done_noop;
4306         }
4307
4308         /* Basic syntax checks are OK. Do the actual settings. */
4309         for ( a=e->e_attrs; a; a=a->a_next ) {
4310                 if ( a == oc_at ) continue;
4311                 ct = config_find_table( colst, nocs, a->a_desc, ca );
4312                 if ( !ct ) continue;    /* user data? */
4313                 for (i=0; a->a_vals[i].bv_val; i++) {
4314                         char *iptr = NULL;
4315                         ca->line = a->a_vals[i].bv_val;
4316                         if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED ) {
4317                                 ptr = strchr( ca->line, '}' );
4318                                 if ( ptr ) {
4319                                         iptr = strchr( ca->line, '{' );
4320                                         ca->line = ptr+1;
4321                                 }
4322                         }
4323                         if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED_SIB ) {
4324                                 if ( iptr ) {
4325                                         ca->valx = strtol( iptr+1, NULL, 0 );
4326                                 } else {
4327                                         ca->valx = -1;
4328                                 }
4329                         } else {
4330                                 ca->valx = i;
4331                         }
4332                         rc = config_parse_add( ct, ca, i );
4333                         if ( rc ) {
4334                                 rc = LDAP_OTHER;
4335                                 goto done;
4336                         }
4337                 }
4338         }
4339 ok:
4340         /* Newly added databases and overlays need to be started up */
4341         if ( CONFIG_ONLINE_ADD( ca )) {
4342                 if ( colst[0]->co_type == Cft_Database ) {
4343                         rc = backend_startup_one( ca->be, &ca->reply );
4344
4345                 } else if ( colst[0]->co_type == Cft_Overlay ) {
4346                         if ( ca->bi->bi_db_open ) {
4347                                 BackendInfo *bi_orig = ca->be->bd_info;
4348                                 ca->be->bd_info = ca->bi;
4349                                 rc = ca->bi->bi_db_open( ca->be, &ca->reply );
4350                                 ca->be->bd_info = bi_orig;
4351                         }
4352                 } else if ( ca->cleanup ) {
4353                         rc = ca->cleanup( ca );
4354                 }
4355                 if ( rc ) {
4356                         if (ca->cr_msg[0] == '\0')
4357                                 snprintf( ca->cr_msg, sizeof( ca->cr_msg ), "<%s> failed startup", ca->argv[0] );
4358
4359                         Debug(LDAP_DEBUG_ANY, "%s: %s (%s)!\n",
4360                                 ca->log, ca->cr_msg, ca->argv[1] );
4361                         rc = LDAP_OTHER;
4362                         goto done;
4363                 }
4364         }
4365
4366         ca->valx = ibase;
4367         ce = ch_calloc( 1, sizeof(CfEntryInfo) );
4368         ce->ce_parent = last;
4369         ce->ce_entry = entry_dup( e );
4370         ce->ce_entry->e_private = ce;
4371         ce->ce_type = colst[0]->co_type;
4372         ce->ce_be = ca->be;
4373         ce->ce_bi = ca->bi;
4374         ce->ce_private = ca->private;
4375         ca->ca_entry = ce->ce_entry;
4376         if ( !last ) {
4377                 cfb->cb_root = ce;
4378         } else if ( last->ce_kids ) {
4379                 CfEntryInfo *c2, **cprev;
4380
4381                 /* Advance to first of this type */
4382                 cprev = &last->ce_kids;
4383                 for ( c2 = *cprev; c2 && c2->ce_type < ce->ce_type; ) {
4384                         cprev = &c2->ce_sibs;
4385                         c2 = c2->ce_sibs;
4386                 }
4387                 /* Account for the (-1) frontendDB entry */
4388                 if ( ce->ce_type == Cft_Database ) {
4389                         if ( ca->be == frontendDB )
4390                                 ibase = 0;
4391                         else if ( ibase != -1 )
4392                                 ibase++;
4393                 }
4394                 /* Append */
4395                 if ( ibase < 0 ) {
4396                         for (c2 = *cprev; c2 && c2->ce_type == ce->ce_type;) {
4397                                 cprev = &c2->ce_sibs;
4398                                 c2 = c2->ce_sibs;
4399                         }
4400                 } else {
4401                 /* Insert */
4402                         int i;
4403                         for ( i=0; i<ibase; i++ ) {
4404                                 c2 = *cprev;
4405                                 cprev = &c2->ce_sibs;
4406                         }
4407                 }
4408                 ce->ce_sibs = *cprev;
4409                 *cprev = ce;
4410         } else {
4411                 last->ce_kids = ce;
4412         }
4413
4414 done:
4415         if ( rc ) {
4416                 if ( (colst[0]->co_type == Cft_Database) && ca->be ) {
4417                         if ( ca->be != frontendDB )
4418                                 backend_destroy_one( ca->be, 1 );
4419                 } else if ( (colst[0]->co_type == Cft_Overlay) && ca->bi ) {
4420                         overlay_destroy_one( ca->be, (slap_overinst *)ca->bi );
4421                 } else if ( colst[0]->co_type == Cft_Schema ) {
4422                         schema_destroy_one( ca, colst, nocs, last );
4423                 }
4424         }
4425 done_noop:
4426
4427         ch_free( ca->argv );
4428         if ( colst ) ch_free( colst );
4429         return rc;
4430 }
4431
4432 #define BIGTMP  10000
4433 static int
4434 config_rename_add( Operation *op, SlapReply *rs, CfEntryInfo *ce,
4435         int base, int rebase, int max, int use_ldif )
4436 {
4437         CfEntryInfo *ce2, *ce3, *cetmp = NULL, *cerem = NULL;
4438         ConfigType etype = ce->ce_type;
4439         int count = 0, rc = 0;
4440
4441         /* Reverse ce list */
4442         for (ce2 = ce->ce_sibs;ce2;ce2 = ce3) {
4443                 if (ce2->ce_type != etype) {
4444                         cerem = ce2;
4445                         break;
4446                 }
4447                 ce3 = ce2->ce_sibs;
4448                 ce2->ce_sibs = cetmp;
4449                 cetmp = ce2;
4450                 count++;
4451                 if ( max && count >= max ) {
4452                         cerem = ce3;
4453                         break;
4454                 }
4455         }
4456
4457         /* Move original to a temp name until increments are done */
4458         if ( rebase ) {
4459                 ce->ce_entry->e_private = NULL;
4460                 rc = config_renumber_one( op, rs, ce->ce_parent, ce->ce_entry,
4461                         base+BIGTMP, 0, use_ldif );
4462                 ce->ce_entry->e_private = ce;
4463         }
4464         /* start incrementing */
4465         for (ce2=cetmp; ce2; ce2=ce3) {
4466                 ce3 = ce2->ce_sibs;
4467                 ce2->ce_sibs = cerem;
4468                 cerem = ce2;
4469                 if ( rc == 0 ) 
4470                         rc = config_renumber_one( op, rs, ce2->ce_parent, ce2->ce_entry,
4471                                 count+base, 0, use_ldif );
4472                 count--;
4473         }
4474         if ( rebase )
4475                 rc = config_renumber_one( op, rs, ce->ce_parent, ce->ce_entry,
4476                         base, 0, use_ldif );
4477         return rc;
4478 }
4479
4480 static int
4481 config_rename_del( Operation *op, SlapReply *rs, CfEntryInfo *ce,
4482         CfEntryInfo *ce2, int old, int use_ldif )
4483 {
4484         int count = 0;
4485
4486         /* Renumber original to a temp value */
4487         ce->ce_entry->e_private = NULL;
4488         config_renumber_one( op, rs, ce->ce_parent, ce->ce_entry,
4489                 old+BIGTMP, 0, use_ldif );
4490         ce->ce_entry->e_private = ce;
4491
4492         /* start decrementing */
4493         for (; ce2 != ce; ce2=ce2->ce_sibs) {
4494                 config_renumber_one( op, rs, ce2->ce_parent, ce2->ce_entry,
4495                         count+old, 0, use_ldif );
4496                 count++;
4497         }
4498         return config_renumber_one( op, rs, ce->ce_parent, ce->ce_entry,
4499                 count+old, 0, use_ldif );
4500 }
4501
4502 /* Parse an LDAP entry into config directives, then store in underlying
4503  * database.
4504  */
4505 static int
4506 config_back_add( Operation *op, SlapReply *rs )
4507 {
4508         CfBackInfo *cfb;
4509         int renumber;
4510         ConfigArgs ca;
4511
4512         if ( !access_allowed( op, op->ora_e, slap_schema.si_ad_entry,
4513                 NULL, ACL_WADD, NULL )) {
4514                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
4515                 goto out;
4516         }
4517
4518         cfb = (CfBackInfo *)op->o_bd->be_private;
4519
4520         /* add opattrs for syncprov */
4521         {
4522                 char textbuf[SLAP_TEXT_BUFLEN];
4523                 size_t textlen = sizeof textbuf;
4524                 rs->sr_err = entry_schema_check(op, op->ora_e, NULL, 0, 1,
4525                         &rs->sr_text, textbuf, sizeof( textbuf ) );
4526                 if ( rs->sr_err != LDAP_SUCCESS )
4527                         goto out;
4528                 rs->sr_err = slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
4529                 if ( rs->sr_err != LDAP_SUCCESS ) {
4530                         Debug( LDAP_DEBUG_TRACE,
4531                                 LDAP_XSTRING(config_back_add) ": entry failed op attrs add: "
4532                                 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
4533                         goto out;
4534                 }
4535         }
4536
4537         ldap_pvt_thread_pool_pause( &connection_pool );
4538
4539         /* Strategy:
4540          * 1) check for existence of entry
4541          * 2) check for sibling renumbering
4542          * 3) perform internal add
4543          * 4) perform any necessary renumbering
4544          * 5) store entry in underlying database
4545          */
4546         rs->sr_err = config_add_internal( cfb, op->ora_e, &ca, rs, &renumber, op );
4547         if ( rs->sr_err != LDAP_SUCCESS ) {
4548                 rs->sr_text = ca.cr_msg;
4549                 goto out2;
4550         }
4551
4552         if ( renumber ) {
4553                 CfEntryInfo *ce = ca.ca_entry->e_private;
4554                 req_add_s addr = op->oq_add;
4555                 op->o_tag = LDAP_REQ_MODRDN;
4556                 rs->sr_err = config_rename_add( op, rs, ce, ca.valx, 0, 0, cfb->cb_use_ldif );
4557                 op->o_tag = LDAP_REQ_ADD;
4558                 op->oq_add = addr;
4559                 if ( rs->sr_err != LDAP_SUCCESS ) {
4560                         goto out2;
4561                 }
4562         }
4563
4564         if ( cfb->cb_use_ldif ) {
4565                 BackendDB *be = op->o_bd;
4566                 slap_callback sc = { NULL, slap_null_cb, NULL, NULL }, *scp;
4567                 struct berval dn, ndn;
4568
4569                 op->o_bd = &cfb->cb_db;
4570
4571                 /* Save current rootdn; use the underlying DB's rootdn */
4572                 dn = op->o_dn;
4573                 ndn = op->o_ndn;
4574                 op->o_dn = op->o_bd->be_rootdn;
4575                 op->o_ndn = op->o_bd->be_rootndn;
4576
4577                 scp = op->o_callback;
4578                 op->o_callback = &sc;
4579                 op->o_bd->be_add( op, rs );
4580                 op->o_bd = be;
4581                 op->o_callback = scp;
4582                 op->o_dn = dn;
4583                 op->o_ndn = ndn;
4584         }
4585
4586 out2:;
4587         ldap_pvt_thread_pool_resume( &connection_pool );
4588
4589 out:;
4590         send_ldap_result( op, rs );
4591         slap_graduate_commit_csn( op );
4592         return rs->sr_err;
4593 }
4594
4595 typedef struct delrec {
4596         struct delrec *next;
4597         int nidx;
4598         int idx[1];
4599 } delrec;
4600
4601 static int
4602 config_modify_add( ConfigTable *ct, ConfigArgs *ca, AttributeDescription *ad,
4603         int i )
4604 {
4605         int rc;
4606
4607         if (ad->ad_type->sat_flags & SLAP_AT_ORDERED &&
4608                 ca->line[0] == '{' )
4609         {
4610                 char *ptr = strchr( ca->line + 1, '}' );
4611                 if ( ptr ) {
4612                         char    *next;
4613
4614                         ca->valx = strtol( ca->line + 1, &next, 0 );
4615                         if ( next == ca->line + 1 || next[ 0 ] != '}' ) {
4616                                 return LDAP_OTHER;
4617                         }
4618                         ca->line = ptr+1;
4619                 }
4620         }
4621         rc = config_parse_add( ct, ca, i );
4622         if ( rc ) {
4623                 rc = LDAP_OTHER;
4624         }
4625         return rc;
4626 }
4627
4628 static int
4629 config_modify_internal( CfEntryInfo *ce, Operation *op, SlapReply *rs,
4630         ConfigArgs *ca )
4631 {
4632         int rc = LDAP_UNWILLING_TO_PERFORM;
4633         Modifications *ml;
4634         Entry *e = ce->ce_entry;
4635         Attribute *save_attrs = e->e_attrs, *oc_at, *s, *a;
4636         ConfigTable *ct;
4637         ConfigOCs **colst;
4638         int i, nocs;
4639         char *ptr;
4640         delrec *dels = NULL, *deltail = NULL;
4641
4642         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
4643         if ( !oc_at ) return LDAP_OBJECT_CLASS_VIOLATION;
4644
4645         colst = count_ocs( oc_at, &nocs );
4646
4647         /* make sure add/del flags are clear; should always be true */
4648         for ( s = save_attrs; s; s = s->a_next ) {
4649                 s->a_flags &= ~(SLAP_ATTR_IXADD|SLAP_ATTR_IXDEL);
4650         }
4651
4652         e->e_attrs = attrs_dup( e->e_attrs );
4653
4654         init_config_argv( ca );
4655         ca->be = ce->ce_be;
4656         ca->bi = ce->ce_bi;
4657         ca->private = ce->ce_private;
4658         ca->ca_entry = e;
4659         ca->fname = "slapd";
4660         ca->ca_op = op;
4661         strcpy( ca->log, "back-config" );
4662
4663         for (ml = op->orm_modlist; ml; ml=ml->sml_next) {
4664                 ct = config_find_table( colst, nocs, ml->sml_desc, ca );
4665                 switch (ml->sml_op) {
4666                 case LDAP_MOD_DELETE:
4667                 case LDAP_MOD_REPLACE: {
4668                         BerVarray vals = NULL, nvals = NULL;
4669                         int *idx = NULL;
4670                         if ( ct && ( ct->arg_type & ARG_NO_DELETE )) {
4671                                 rc = LDAP_OTHER;
4672                                 snprintf(ca->cr_msg, sizeof(ca->cr_msg), "cannot delete %s",
4673                                         ml->sml_desc->ad_cname.bv_val );
4674                                 goto out_noop;
4675                         }
4676                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
4677                                 vals = ml->sml_values;
4678                                 nvals = ml->sml_nvalues;
4679                                 ml->sml_values = NULL;
4680                                 ml->sml_nvalues = NULL;
4681                         }
4682                         /* If we're deleting by values, remember the indexes of the
4683                          * values we deleted.
4684                          */
4685                         if ( ct && ml->sml_values ) {
4686                                 delrec *d;
4687                                 i = ml->sml_numvals;
4688                                 d = ch_malloc( sizeof(delrec) + (i - 1)* sizeof(int));
4689                                 d->nidx = i;
4690                                 d->next = NULL;
4691                                 if ( dels ) {
4692                                         deltail->next = d;
4693                                 } else {
4694                                         dels = d;
4695                                 }
4696                                 deltail = d;
4697                                 idx = d->idx;
4698                         }
4699                         rc = modify_delete_vindex(e, &ml->sml_mod,
4700                                 get_permissiveModify(op),
4701                                 &rs->sr_text, ca->cr_msg, sizeof(ca->cr_msg), idx );
4702                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
4703                                 ml->sml_values = vals;
4704                                 ml->sml_nvalues = nvals;
4705                         }
4706                         if ( !vals )
4707                                 break;
4708                         }
4709                         /* FALLTHRU: LDAP_MOD_REPLACE && vals */
4710
4711                 case LDAP_MOD_ADD:
4712                 case SLAP_MOD_SOFTADD: {
4713                         int mop = ml->sml_op;
4714                         int navals = -1;
4715                         ml->sml_op = LDAP_MOD_ADD;
4716                         if ( ct ) {
4717                                 if ( ct->arg_type & ARG_NO_INSERT ) {
4718                                         Attribute *a = attr_find( e->e_attrs, ml->sml_desc );
4719                                         if ( a ) {
4720                                                 navals = a->a_numvals;
4721                                         }
4722                                 }
4723                                 for ( i=0; !BER_BVISNULL( &ml->sml_values[i] ); i++ ) {
4724                                         if ( ml->sml_values[i].bv_val[0] == '{' &&
4725                                                 navals >= 0 )
4726                                         {
4727                                                 char    *next, *val = ml->sml_values[i].bv_val + 1;
4728                                                 int     j;
4729
4730                                                 j = strtol( val, &next, 0 );
4731                                                 if ( next == val || next[ 0 ] != '}' || j < navals ) {
4732                                                         rc = LDAP_OTHER;
4733                                                         snprintf(ca->cr_msg, sizeof(ca->cr_msg), "cannot insert %s",
4734                                                                 ml->sml_desc->ad_cname.bv_val );
4735                                                         goto out_noop;
4736                                                 }
4737                                         }
4738                                         rc = check_vals( ct, ca, ml, 0 );
4739                                         if ( rc ) goto out_noop;
4740                                 }
4741                         }
4742                         rc = modify_add_values(e, &ml->sml_mod,
4743                                    get_permissiveModify(op),
4744                                    &rs->sr_text, ca->cr_msg, sizeof(ca->cr_msg) );
4745
4746                         /* If value already exists, show success here
4747                          * and ignore this operation down below.
4748                          */
4749                         if ( mop == SLAP_MOD_SOFTADD ) {
4750                                 if ( rc == LDAP_TYPE_OR_VALUE_EXISTS )
4751                                         rc = LDAP_SUCCESS;
4752                                 else
4753                                         mop = LDAP_MOD_ADD;
4754                         }
4755                         ml->sml_op = mop;
4756                         break;
4757                         }
4758
4759                         break;
4760                 case LDAP_MOD_INCREMENT:        /* FIXME */
4761                         break;
4762                 default:
4763                         break;
4764                 }
4765                 if(rc != LDAP_SUCCESS) break;
4766         }
4767         
4768         if ( rc == LDAP_SUCCESS) {
4769                 /* check that the entry still obeys the schema */
4770                 rc = entry_schema_check(op, e, NULL, 0, 0,
4771                         &rs->sr_text, ca->cr_msg, sizeof(ca->cr_msg) );
4772                 if ( rc ) goto out_noop;
4773         }
4774         /* Basic syntax checks are OK. Do the actual settings. */
4775         for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
4776                 ct = config_find_table( colst, nocs, ml->sml_desc, ca );
4777                 if ( !ct ) continue;
4778
4779                 s = attr_find( save_attrs, ml->sml_desc );
4780                 a = attr_find( e->e_attrs, ml->sml_desc );
4781
4782                 switch (ml->sml_op) {
4783                 case LDAP_MOD_DELETE:
4784                 case LDAP_MOD_REPLACE: {
4785                         BerVarray vals = NULL, nvals = NULL;
4786                         delrec *d = NULL;
4787
4788                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
4789                                 vals = ml->sml_values;
4790                                 nvals = ml->sml_nvalues;
4791                                 ml->sml_values = NULL;
4792                                 ml->sml_nvalues = NULL;
4793                         }
4794
4795                         if ( ml->sml_values )
4796                                 d = dels;
4797
4798                         /* If we didn't delete the whole attribute */
4799                         if ( ml->sml_values && a ) {
4800                                 struct berval *mvals;
4801                                 int j;
4802
4803                                 if ( ml->sml_nvalues )
4804                                         mvals = ml->sml_nvalues;
4805                                 else
4806                                         mvals = ml->sml_values;
4807
4808                                 /* use the indexes we saved up above */
4809                                 for (i=0; i < d->nidx; i++) {
4810                                         struct berval bv = *mvals++;
4811                                         if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED &&
4812                                                 bv.bv_val[0] == '{' ) {
4813                                                 ptr = strchr( bv.bv_val, '}' ) + 1;
4814                                                 bv.bv_len -= ptr - bv.bv_val;
4815                                                 bv.bv_val = ptr;
4816                                         }
4817                                         ca->line = bv.bv_val;
4818                                         ca->valx = d->idx[i];
4819                                         rc = config_del_vals( ct, ca );
4820                                         if ( rc != LDAP_SUCCESS ) break;
4821                                         if ( s )
4822                                                 s->a_flags |= SLAP_ATTR_IXDEL;
4823                                         for (j=i+1; j < d->nidx; j++)
4824                                                 if ( d->idx[j] >d->idx[i] )
4825                                                         d->idx[j]--;
4826                                 }
4827                         } else {
4828                                 ca->valx = -1;
4829                                 ca->line = NULL;
4830                                 rc = config_del_vals( ct, ca );
4831                                 if ( rc ) rc = LDAP_OTHER;
4832                                 if ( s )
4833                                         s->a_flags |= SLAP_ATTR_IXDEL;
4834                         }
4835                         if ( ml->sml_values ) {
4836                                 d = d->next;
4837                                 ch_free( dels );
4838                                 dels = d;
4839                         }
4840                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
4841                                 ml->sml_values = vals;
4842                                 ml->sml_nvalues = nvals;
4843                         }
4844                         if ( !vals || rc != LDAP_SUCCESS )
4845                                 break;
4846                         }
4847                         /* FALLTHRU: LDAP_MOD_REPLACE && vals */
4848
4849                 case LDAP_MOD_ADD:
4850                         for (i=0; ml->sml_values[i].bv_val; i++) {
4851                                 ca->line = ml->sml_values[i].bv_val;
4852                                 ca->valx = -1;
4853                                 rc = config_modify_add( ct, ca, ml->sml_desc, i );
4854                                 if ( rc )
4855                                         goto out;
4856                                 a->a_flags |= SLAP_ATTR_IXADD;
4857                         }
4858                         break;
4859                 }
4860         }
4861
4862 out:
4863         /* Undo for a failed operation */
4864         if ( rc != LDAP_SUCCESS ) {
4865                 ConfigReply msg = ca->reply;
4866                 for ( s = save_attrs; s; s = s->a_next ) {
4867                         if ( s->a_flags & SLAP_ATTR_IXDEL ) {
4868                                 s->a_flags &= ~(SLAP_ATTR_IXDEL|SLAP_ATTR_IXADD);
4869                                 ct = config_find_table( colst, nocs, s->a_desc, ca );
4870                                 a = attr_find( e->e_attrs, s->a_desc );
4871                                 if ( a ) {
4872                                         /* clear the flag so the add check below will skip it */
4873                                         a->a_flags &= ~(SLAP_ATTR_IXDEL|SLAP_ATTR_IXADD);
4874                                         ca->valx = -1;
4875                                         ca->line = NULL;
4876                                         config_del_vals( ct, ca );
4877                                 }
4878                                 for ( i=0; !BER_BVISNULL( &s->a_vals[i] ); i++ ) {
4879                                         ca->line = s->a_vals[i].bv_val;
4880                                         ca->valx = -1;
4881                                         config_modify_add( ct, ca, s->a_desc, i );
4882                                 }
4883                         }
4884                 }
4885                 for ( a = e->e_attrs; a; a = a->a_next ) {
4886                         if ( a->a_flags & SLAP_ATTR_IXADD ) {
4887                                 ct = config_find_table( colst, nocs, a->a_desc, ca );
4888                                 ca->valx = -1;
4889                                 ca->line = NULL;
4890                                 config_del_vals( ct, ca );
4891                                 s = attr_find( save_attrs, a->a_desc );
4892                                 if ( s ) {
4893                                         s->a_flags &= ~(SLAP_ATTR_IXDEL|SLAP_ATTR_IXADD);
4894                                         for ( i=0; !BER_BVISNULL( &s->a_vals[i] ); i++ ) {
4895                                                 ca->line = s->a_vals[i].bv_val;
4896                                                 ca->valx = -1;
4897                                                 config_modify_add( ct, ca, s->a_desc, i );
4898                                         }
4899                                 }
4900                         }
4901                 }
4902                 ca->reply = msg;
4903         }
4904
4905         if ( ca->cleanup )
4906                 ca->cleanup( ca );
4907 out_noop:
4908         if ( rc == LDAP_SUCCESS ) {
4909                 attrs_free( save_attrs );
4910         } else {
4911                 attrs_free( e->e_attrs );
4912                 e->e_attrs = save_attrs;
4913         }
4914         ch_free( ca->argv );
4915         if ( colst ) ch_free( colst );
4916         while( dels ) {
4917                 deltail = dels->next;
4918                 ch_free( dels );
4919                 dels = deltail;
4920         }
4921
4922         return rc;
4923 }
4924
4925 static int
4926 config_back_modify( Operation *op, SlapReply *rs )
4927 {
4928         CfBackInfo *cfb;
4929         CfEntryInfo *ce, *last;
4930         Modifications *ml;
4931         ConfigArgs ca = {0};
4932         struct berval rdn;
4933         char *ptr;
4934         AttributeDescription *rad = NULL;
4935         int do_pause = 1;
4936
4937         cfb = (CfBackInfo *)op->o_bd->be_private;
4938
4939         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
4940         if ( !ce ) {
4941                 if ( last )
4942                         rs->sr_matched = last->ce_entry->e_name.bv_val;
4943                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
4944                 goto out;
4945         }
4946
4947         if ( !acl_check_modlist( op, ce->ce_entry, op->orm_modlist )) {
4948                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
4949                 goto out;
4950         }
4951
4952         /* Get type of RDN */
4953         rdn = ce->ce_entry->e_nname;
4954         ptr = strchr( rdn.bv_val, '=' );
4955         rdn.bv_len = ptr - rdn.bv_val;
4956         slap_bv2ad( &rdn, &rad, &rs->sr_text );
4957
4958         /* Some basic validation... */
4959         for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
4960                 /* Don't allow Modify of RDN; must use ModRdn for that. */
4961                 if ( ml->sml_desc == rad ) {
4962                         rs->sr_err = LDAP_NOT_ALLOWED_ON_RDN;
4963                         rs->sr_text = "Use modrdn to change the entry name";
4964                         goto out;
4965                 }
4966                 /* Internal update of contextCSN? */
4967                 if ( ml->sml_desc == slap_schema.si_ad_contextCSN && op->o_conn->c_conn_idx == -1 ) {
4968                         do_pause = 0;
4969                         break;
4970                 }
4971         }
4972
4973         slap_mods_opattrs( op, &op->orm_modlist, 1 );
4974
4975         if ( do_pause )
4976                 ldap_pvt_thread_pool_pause( &connection_pool );
4977
4978         /* Strategy:
4979          * 1) perform the Modify on the cached Entry.
4980          * 2) verify that the Entry still satisfies the schema.
4981          * 3) perform the individual config operations.
4982          * 4) store Modified entry in underlying LDIF backend.
4983          */
4984         rs->sr_err = config_modify_internal( ce, op, rs, &ca );
4985         if ( rs->sr_err ) {
4986                 rs->sr_text = ca.cr_msg;
4987         } else if ( cfb->cb_use_ldif ) {
4988                 BackendDB *be = op->o_bd;
4989                 slap_callback sc = { NULL, slap_null_cb, NULL, NULL }, *scp;
4990                 struct berval dn, ndn;
4991
4992                 op->o_bd = &cfb->cb_db;
4993
4994                 dn = op->o_dn;
4995                 ndn = op->o_ndn;
4996                 op->o_dn = op->o_bd->be_rootdn;
4997                 op->o_ndn = op->o_bd->be_rootndn;
4998
4999                 scp = op->o_callback;
5000                 op->o_callback = &sc;
5001                 op->o_bd->be_modify( op, rs );
5002                 op->o_bd = be;
5003                 op->o_callback = scp;
5004                 op->o_dn = dn;
5005                 op->o_ndn = ndn;
5006         }
5007
5008         if ( do_pause )
5009                 ldap_pvt_thread_pool_resume( &connection_pool );
5010 out:
5011         send_ldap_result( op, rs );
5012         slap_graduate_commit_csn( op );
5013         return rs->sr_err;
5014 }
5015
5016 static int
5017 config_back_modrdn( Operation *op, SlapReply *rs )
5018 {
5019         CfBackInfo *cfb;
5020         CfEntryInfo *ce, *last;
5021         struct berval rdn;
5022         int ixold, ixnew;
5023
5024         cfb = (CfBackInfo *)op->o_bd->be_private;
5025
5026         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
5027         if ( !ce ) {
5028                 if ( last )
5029                         rs->sr_matched = last->ce_entry->e_name.bv_val;
5030                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
5031                 goto out;
5032         }
5033         if ( !access_allowed( op, ce->ce_entry, slap_schema.si_ad_entry,
5034                 NULL, ACL_WRITE, NULL )) {
5035                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
5036                 goto out;
5037         }
5038         { Entry *parent;
5039                 if ( ce->ce_parent )
5040                         parent = ce->ce_parent->ce_entry;
5041                 else
5042                         parent = (Entry *)&slap_entry_root;
5043                 if ( !access_allowed( op, parent, slap_schema.si_ad_children,
5044                         NULL, ACL_WRITE, NULL )) {
5045                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
5046                         goto out;
5047                 }
5048         }
5049
5050         /* We don't allow moving objects to new parents.
5051          * Generally we only allow reordering a set of ordered entries.
5052          */
5053         if ( op->orr_newSup ) {
5054                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5055                 goto out;
5056         }
5057
5058         /* If newRDN == oldRDN, quietly succeed */
5059         dnRdn( &op->o_req_ndn, &rdn );
5060         if ( dn_match( &rdn, &op->orr_nnewrdn )) {
5061                 rs->sr_err = LDAP_SUCCESS;
5062                 goto out;
5063         }
5064
5065         /* Current behavior, subject to change as needed:
5066          *
5067          * For backends and overlays, we only allow renumbering.
5068          * For schema, we allow renaming with the same number.
5069          * Otherwise, the op is not allowed.
5070          */
5071
5072         if ( ce->ce_type == Cft_Schema ) {
5073                 char *ptr1, *ptr2;
5074                 int len;
5075
5076                 /* Can't alter the main cn=schema entry */
5077                 if ( ce->ce_parent->ce_type == Cft_Global ) {
5078                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5079                         rs->sr_text = "renaming not allowed for this entry";
5080                         goto out;
5081                 }
5082
5083                 /* We could support this later if desired */
5084                 ptr1 = ber_bvchr( &rdn, '}' );
5085                 ptr2 = ber_bvchr( &op->orr_newrdn, '}' );
5086                 len = ptr1 - rdn.bv_val;
5087                 if ( len != ptr2 - op->orr_newrdn.bv_val ||
5088                         strncmp( rdn.bv_val, op->orr_newrdn.bv_val, len )) {
5089                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5090                         rs->sr_text = "schema reordering not supported";
5091                         goto out;
5092                 }
5093         } else if ( ce->ce_type == Cft_Database ||
5094                 ce->ce_type == Cft_Overlay ) {
5095                 char *ptr1, *ptr2, *iptr1, *iptr2;
5096                 int len1, len2;
5097
5098                 iptr2 = ber_bvchr( &op->orr_newrdn, '=' ) + 1;
5099                 if ( *iptr2 != '{' ) {
5100                         rs->sr_err = LDAP_NAMING_VIOLATION;
5101                         rs->sr_text = "new ordering index is required";
5102                         goto out;
5103                 }
5104                 iptr2++;
5105                 iptr1 = ber_bvchr( &rdn, '{' ) + 1;
5106                 ptr1 = ber_bvchr( &rdn, '}' );
5107                 ptr2 = ber_bvchr( &op->orr_newrdn, '}' );
5108                 if ( !ptr2 ) {
5109                         rs->sr_err = LDAP_NAMING_VIOLATION;
5110                         rs->sr_text = "new ordering index is required";
5111                         goto out;
5112                 }
5113
5114                 len1 = ptr1 - rdn.bv_val;
5115                 len2 = ptr2 - op->orr_newrdn.bv_val;
5116
5117                 if ( rdn.bv_len - len1 != op->orr_newrdn.bv_len - len2 ||
5118                         strncmp( ptr1, ptr2, rdn.bv_len - len1 )) {
5119                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5120                         rs->sr_text = "changing database/overlay type not allowed";
5121                         goto out;
5122                 }
5123                 ixold = strtol( iptr1, NULL, 0 );
5124                 ixnew = strtol( iptr2, &ptr1, 0 );
5125                 if ( ptr1 != ptr2 || ixold < 0 || ixnew < 0 ) {
5126                         rs->sr_err = LDAP_NAMING_VIOLATION;
5127                         goto out;
5128                 }
5129                 /* config DB is always 0, cannot be changed */
5130                 if ( ce->ce_type == Cft_Database && ( ixold == 0 || ixnew == 0 )) {
5131                         rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
5132                         goto out;
5133                 }
5134         } else {
5135                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5136                 rs->sr_text = "renaming not supported for this entry";
5137                 goto out;
5138         }
5139
5140         ldap_pvt_thread_pool_pause( &connection_pool );
5141
5142         if ( ce->ce_type == Cft_Schema ) {
5143                 req_modrdn_s modr = op->oq_modrdn;
5144                 struct berval rdn;
5145                 Attribute *a;
5146                 rs->sr_err = config_rename_attr( rs, ce->ce_entry, &rdn, &a );
5147                 if ( rs->sr_err == LDAP_SUCCESS ) {
5148                         rs->sr_err = config_rename_one( op, rs, ce->ce_entry,
5149                                 ce->ce_parent, a, &op->orr_newrdn, &op->orr_nnewrdn,
5150                                 cfb->cb_use_ldif );
5151                 }
5152                 op->oq_modrdn = modr;
5153         } else {
5154                 CfEntryInfo *ce2, *cebase, **cprev, **cbprev, *ceold;
5155                 req_modrdn_s modr = op->oq_modrdn;
5156                 int i;
5157
5158                 /* Advance to first of this type */
5159                 cprev = &ce->ce_parent->ce_kids;
5160                 for ( ce2 = *cprev; ce2 && ce2->ce_type != ce->ce_type; ) {
5161                         cprev = &ce2->ce_sibs;
5162                         ce2 = ce2->ce_sibs;
5163                 }
5164                 /* Skip the -1 entry */
5165                 if ( ce->ce_type == Cft_Database ) {
5166                         cprev = &ce2->ce_sibs;
5167                         ce2 = ce2->ce_sibs;
5168                 }
5169                 cebase = ce2;
5170                 cbprev = cprev;
5171
5172                 /* Remove from old slot */
5173                 for ( ce2 = *cprev; ce2 && ce2 != ce; ce2 = ce2->ce_sibs )
5174                         cprev = &ce2->ce_sibs;
5175                 *cprev = ce->ce_sibs;
5176                 ceold = ce->ce_sibs;
5177
5178                 /* Insert into new slot */
5179                 cprev = cbprev;
5180                 for ( i=0; i<ixnew; i++ ) {
5181                         ce2 = *cprev;
5182                         if ( !ce2 )
5183                                 break;
5184                         cprev = &ce2->ce_sibs;
5185                 }
5186                 ce->ce_sibs = *cprev;
5187                 *cprev = ce;
5188
5189                 ixnew = i;
5190
5191                 /* NOTE: These should be encoded in the OC tables, not inline here */
5192                 if ( ce->ce_type == Cft_Database )
5193                         backend_db_move( ce->ce_be, ixnew );
5194                 else if ( ce->ce_type == Cft_Overlay )
5195                         overlay_move( ce->ce_be, (slap_overinst *)ce->ce_bi, ixnew );
5196                         
5197                 if ( ixold < ixnew ) {
5198                         rs->sr_err = config_rename_del( op, rs, ce, ceold, ixold,
5199                                 cfb->cb_use_ldif );
5200                 } else {
5201                         rs->sr_err = config_rename_add( op, rs, ce, ixnew, 1,
5202                                 ixold - ixnew, cfb->cb_use_ldif );
5203                 }
5204                 op->oq_modrdn = modr;
5205         }
5206
5207         ldap_pvt_thread_pool_resume( &connection_pool );
5208 out:
5209         send_ldap_result( op, rs );
5210         return rs->sr_err;
5211 }
5212
5213 static int
5214 config_back_delete( Operation *op, SlapReply *rs )
5215 {
5216         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM, NULL );
5217         return rs->sr_err;
5218 }
5219
5220 static int
5221 config_back_search( Operation *op, SlapReply *rs )
5222 {
5223         CfBackInfo *cfb;
5224         CfEntryInfo *ce, *last;
5225         slap_mask_t mask;
5226
5227         cfb = (CfBackInfo *)op->o_bd->be_private;
5228
5229         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
5230         if ( !ce ) {
5231                 if ( last )
5232                         rs->sr_matched = last->ce_entry->e_name.bv_val;
5233                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
5234                 goto out;
5235         }
5236         if ( !access_allowed_mask( op, ce->ce_entry, slap_schema.si_ad_entry, NULL,
5237                 ACL_SEARCH, NULL, &mask ))
5238         {
5239                 if ( !ACL_GRANT( mask, ACL_DISCLOSE )) {
5240                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
5241                 } else {
5242                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
5243                 }
5244                 goto out;
5245         }
5246         switch ( op->ors_scope ) {
5247         case LDAP_SCOPE_BASE:
5248         case LDAP_SCOPE_SUBTREE:
5249                 config_send( op, rs, ce, 0 );
5250                 break;
5251                 
5252         case LDAP_SCOPE_ONELEVEL:
5253                 for (ce = ce->ce_kids; ce; ce=ce->ce_sibs) {
5254                         config_send( op, rs, ce, 1 );
5255                 }
5256                 break;
5257         }
5258                 
5259         rs->sr_err = LDAP_SUCCESS;
5260 out:
5261         send_ldap_result( op, rs );
5262         return 0;
5263 }
5264
5265 /* no-op, we never free entries */
5266 int config_entry_release(
5267         Operation *op,
5268         Entry *e,
5269         int rw )
5270 {
5271         if ( !e->e_private ) {
5272                 entry_free( e );
5273         }
5274         return LDAP_SUCCESS;
5275 }
5276
5277 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
5278  */
5279 int config_back_entry_get(
5280         Operation *op,
5281         struct berval *ndn,
5282         ObjectClass *oc,
5283         AttributeDescription *at,
5284         int rw,
5285         Entry **ent )
5286 {
5287         CfBackInfo *cfb;
5288         CfEntryInfo *ce, *last;
5289
5290         cfb = (CfBackInfo *)op->o_bd->be_private;
5291
5292         ce = config_find_base( cfb->cb_root, ndn, &last );
5293         if ( ce ) {
5294                 *ent = ce->ce_entry;
5295                 if ( *ent && oc && !is_entry_objectclass_or_sub( *ent, oc ) ) {
5296                         *ent = NULL;
5297                 }
5298         }
5299
5300         return ( *ent == NULL ? 1 : 0 );
5301 }
5302
5303 static void
5304 config_build_attrs( Entry *e, AttributeType **at, AttributeDescription *ad,
5305         ConfigTable *ct, ConfigArgs *c )
5306 {
5307         int i, rc;
5308
5309         for (; at && *at; at++) {
5310                 /* Skip the naming attr */
5311                 if ((*at)->sat_ad == ad || (*at)->sat_ad == slap_schema.si_ad_cn )
5312                         continue;
5313                 for (i=0;ct[i].name;i++) {
5314                         if (ct[i].ad == (*at)->sat_ad) {
5315                                 rc = config_get_vals(&ct[i], c);
5316                                 /* NOTE: tolerate that config_get_vals()
5317                                  * returns success with no values */
5318                                 if (rc == LDAP_SUCCESS && c->rvalue_vals != NULL ) {
5319                                         if ( c->rvalue_nvals )
5320                                                 attr_merge(e, ct[i].ad, c->rvalue_vals,
5321                                                         c->rvalue_nvals);
5322                                         else
5323                                                 attr_merge_normalize(e, ct[i].ad,
5324                                                         c->rvalue_vals, NULL);
5325                                         ber_bvarray_free( c->rvalue_nvals );
5326                                         ber_bvarray_free( c->rvalue_vals );
5327                                 }
5328                                 break;
5329                         }
5330                 }
5331         }
5332 }
5333
5334 Entry *
5335 config_build_entry( Operation *op, SlapReply *rs, CfEntryInfo *parent,
5336         ConfigArgs *c, struct berval *rdn, ConfigOCs *main, ConfigOCs *extra )
5337 {
5338         Entry *e = entry_alloc();
5339         CfEntryInfo *ce = ch_calloc( 1, sizeof(CfEntryInfo) );
5340         struct berval val;
5341         struct berval ad_name;
5342         AttributeDescription *ad = NULL;
5343         int rc;
5344         char *ptr;
5345         const char *text;
5346         Attribute *oc_at;
5347         struct berval pdn;
5348         ObjectClass *oc;
5349         CfEntryInfo *ceprev = NULL;
5350
5351         Debug( LDAP_DEBUG_TRACE, "config_build_entry: \"%s\"\n", rdn->bv_val, 0, 0);
5352         e->e_private = ce;
5353         ce->ce_entry = e;
5354         ce->ce_type = main->co_type;
5355         ce->ce_parent = parent;
5356         if ( parent ) {
5357                 pdn = parent->ce_entry->e_nname;
5358                 if ( parent->ce_kids )
5359                         for ( ceprev = parent->ce_kids; ceprev->ce_sibs &&
5360                                 ceprev->ce_type <= ce->ce_type;
5361                                 ceprev = ceprev->ce_sibs );
5362         } else {
5363                 BER_BVZERO( &pdn );
5364         }
5365
5366         ce->ce_private = c->private;
5367         ce->ce_be = c->be;
5368         ce->ce_bi = c->bi;
5369
5370         build_new_dn( &e->e_name, &pdn, rdn, NULL );
5371         ber_dupbv( &e->e_nname, &e->e_name );
5372
5373         attr_merge_normalize_one(e, slap_schema.si_ad_objectClass,
5374                 main->co_name, NULL );
5375         if ( extra )
5376                 attr_merge_normalize_one(e, slap_schema.si_ad_objectClass,
5377                         extra->co_name, NULL );
5378         ptr = strchr(rdn->bv_val, '=');
5379         ad_name.bv_val = rdn->bv_val;
5380         ad_name.bv_len = ptr - rdn->bv_val;
5381         rc = slap_bv2ad( &ad_name, &ad, &text );
5382         if ( rc ) {
5383                 return NULL;
5384         }
5385         val.bv_val = ptr+1;
5386         val.bv_len = rdn->bv_len - (val.bv_val - rdn->bv_val);
5387         attr_merge_normalize_one(e, ad, &val, NULL );
5388
5389         oc = main->co_oc;
5390         c->table = main->co_type;
5391         if ( oc->soc_required )
5392                 config_build_attrs( e, oc->soc_required, ad, main->co_table, c );
5393
5394         if ( oc->soc_allowed )
5395                 config_build_attrs( e, oc->soc_allowed, ad, main->co_table, c );
5396
5397         if ( extra ) {
5398                 oc = extra->co_oc;
5399                 c->table = extra->co_type;
5400                 if ( oc->soc_required )
5401                         config_build_attrs( e, oc->soc_required, ad, extra->co_table, c );
5402
5403                 if ( oc->soc_allowed )
5404                         config_build_attrs( e, oc->soc_allowed, ad, extra->co_table, c );
5405         }
5406
5407         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
5408         rc = structural_class(oc_at->a_vals, &oc, NULL, &text, c->cr_msg,
5409                 sizeof(c->cr_msg), op ? op->o_tmpmemctx : NULL );
5410         attr_merge_normalize_one(e, slap_schema.si_ad_structuralObjectClass, &oc->soc_cname, NULL );
5411         if ( op && !op->o_noop ) {
5412                 op->ora_e = e;
5413                 op->ora_modlist = NULL;
5414                 op->o_bd->be_add( op, rs );
5415                 if ( ( rs->sr_err != LDAP_SUCCESS ) 
5416                                 && (rs->sr_err != LDAP_ALREADY_EXISTS) ) {
5417                         return NULL;
5418                 }
5419         }
5420         if ( ceprev ) {
5421                 ce->ce_sibs = ceprev->ce_sibs;
5422                 ceprev->ce_sibs = ce;
5423         } else if ( parent ) {
5424                 ce->ce_sibs = parent->ce_kids;
5425                 parent->ce_kids = ce;
5426         }
5427
5428         return e;
5429 }
5430
5431 static int
5432 config_build_schema_inc( ConfigArgs *c, CfEntryInfo *ceparent,
5433         Operation *op, SlapReply *rs )
5434 {
5435         Entry *e;
5436         ConfigFile *cf = c->private;
5437         char *ptr;
5438         struct berval bv;
5439
5440         for (; cf; cf=cf->c_sibs, c->depth++) {
5441                 if ( !cf->c_at_head && !cf->c_cr_head && !cf->c_oc_head &&
5442                         !cf->c_om_head ) continue;
5443                 c->value_dn.bv_val = c->log;
5444                 LUTIL_SLASHPATH( cf->c_file.bv_val );
5445                 bv.bv_val = strrchr(cf->c_file.bv_val, LDAP_DIRSEP[0]);
5446                 if ( !bv.bv_val ) {
5447                         bv = cf->c_file;
5448                 } else {
5449                         bv.bv_val++;
5450                         bv.bv_len = cf->c_file.bv_len - (bv.bv_val - cf->c_file.bv_val);
5451                 }
5452                 ptr = strchr( bv.bv_val, '.' );
5453                 if ( ptr )
5454                         bv.bv_len = ptr - bv.bv_val;
5455                 c->value_dn.bv_len = snprintf(c->value_dn.bv_val, sizeof( c->log ), "cn=" SLAP_X_ORDERED_FMT, c->depth);
5456                 if ( c->value_dn.bv_len >= sizeof( c->log ) ) {
5457                         /* FIXME: how can indicate error? */
5458                         return -1;
5459                 }
5460                 strncpy( c->value_dn.bv_val + c->value_dn.bv_len, bv.bv_val,
5461                         bv.bv_len );
5462                 c->value_dn.bv_len += bv.bv_len;
5463                 c->value_dn.bv_val[c->value_dn.bv_len] ='\0';
5464
5465                 c->private = cf;
5466                 e = config_build_entry( op, rs, ceparent, c, &c->value_dn,
5467                         &CFOC_SCHEMA, NULL );
5468                 if ( !e ) {
5469                         return -1;
5470                 } else if ( e && cf->c_kids ) {
5471                         c->private = cf->c_kids;
5472                         config_build_schema_inc( c, e->e_private, op, rs );
5473                 }
5474         }
5475         return 0;
5476 }
5477
5478 #ifdef SLAPD_MODULES
5479
5480 static int
5481 config_build_modules( ConfigArgs *c, CfEntryInfo *ceparent,
5482         Operation *op, SlapReply *rs )
5483 {
5484         int i;
5485         ModPaths *mp;
5486
5487         for (i=0, mp=&modpaths; mp; mp=mp->mp_next, i++) {
5488                 if ( BER_BVISNULL( &mp->mp_path ) && !mp->mp_loads )
5489                         continue;
5490                 c->value_dn.bv_val = c->log;
5491                 c->value_dn.bv_len = snprintf(c->value_dn.bv_val, sizeof( c->log ), "cn=module" SLAP_X_ORDERED_FMT, i);
5492                 if ( c->value_dn.bv_len >= sizeof( c->log ) ) {
5493                         /* FIXME: how can indicate error? */
5494                         return -1;
5495                 }
5496                 c->private = mp;
5497                 if ( ! config_build_entry( op, rs, ceparent, c, &c->value_dn, &CFOC_MODULE, NULL )) {
5498                         return -1;
5499                 }
5500         }
5501         return 0;
5502 }
5503 #endif
5504
5505 static int
5506 config_check_schema(Operation *op, CfBackInfo *cfb)
5507 {
5508         struct berval schema_dn = BER_BVC(SCHEMA_RDN "," CONFIG_RDN);
5509         ConfigArgs c = {0};
5510         CfEntryInfo *ce, *last;
5511         Entry *e;
5512
5513         /* If there's no root entry, we must be in the midst of converting */
5514         if ( !cfb->cb_root )
5515                 return 0;
5516
5517         /* Make sure the main schema entry exists */
5518         ce = config_find_base( cfb->cb_root, &schema_dn, &last );
5519         if ( ce ) {
5520                 Attribute *a;
5521                 struct berval *bv;
5522
5523                 e = ce->ce_entry;
5524
5525                 /* Make sure it's up to date */
5526                 if ( cf_om_tail != om_sys_tail ) {
5527                         a = attr_find( e->e_attrs, cfAd_om );
5528                         if ( a ) {
5529                                 if ( a->a_nvals != a->a_vals )
5530                                         ber_bvarray_free( a->a_nvals );
5531                                 ber_bvarray_free( a->a_vals );
5532                                 a->a_vals = NULL;
5533                                 a->a_nvals = NULL;
5534                                 a->a_numvals = 0;
5535                         }
5536                         oidm_unparse( &bv, NULL, NULL, 1 );
5537                         attr_merge_normalize( e, cfAd_om, bv, NULL );
5538                         ber_bvarray_free( bv );
5539                         cf_om_tail = om_sys_tail;
5540                 }
5541                 if ( cf_at_tail != at_sys_tail ) {
5542                         a = attr_find( e->e_attrs, cfAd_attr );
5543                         if ( a ) {
5544                                 if ( a->a_nvals != a->a_vals )
5545                                         ber_bvarray_free( a->a_nvals );
5546                                 ber_bvarray_free( a->a_vals );
5547                                 a->a_vals = NULL;
5548                                 a->a_nvals = NULL;
5549                                 a->a_numvals = 0;
5550                         }
5551                         at_unparse( &bv, NULL, NULL, 1 );
5552                         attr_merge_normalize( e, cfAd_attr, bv, NULL );
5553                         ber_bvarray_free( bv );
5554                         cf_at_tail = at_sys_tail;
5555                 }
5556                 if ( cf_oc_tail != oc_sys_tail ) {
5557                         a = attr_find( e->e_attrs, cfAd_oc );
5558                         if ( a ) {
5559                                 if ( a->a_nvals != a->a_vals )
5560                                         ber_bvarray_free( a->a_nvals );
5561                                 ber_bvarray_free( a->a_vals );
5562                                 a->a_vals = NULL;
5563                                 a->a_nvals = NULL;
5564                                 a->a_numvals = 0;
5565                         }
5566                         oc_unparse( &bv, NULL, NULL, 1 );
5567                         attr_merge_normalize( e, cfAd_oc, bv, NULL );
5568                         ber_bvarray_free( bv );
5569                         cf_oc_tail = oc_sys_tail;
5570                 }
5571         } else {
5572                 SlapReply rs = {REP_RESULT};
5573                 c.private = NULL;
5574                 e = config_build_entry( op, &rs, cfb->cb_root, &c, &schema_rdn,
5575                         &CFOC_SCHEMA, NULL );
5576                 if ( !e ) {
5577                         return -1;
5578                 }
5579                 ce = e->e_private;
5580                 ce->ce_private = cfb->cb_config;
5581                 cf_at_tail = at_sys_tail;
5582                 cf_oc_tail = oc_sys_tail;
5583                 cf_om_tail = om_sys_tail;
5584         }
5585         return 0;
5586 }
5587
5588 static const char *defacl[] = {
5589         NULL, "to", "*", "by", "*", "none", NULL
5590 };
5591
5592 static int
5593 config_back_db_open( BackendDB *be, ConfigReply *cr )
5594 {
5595         CfBackInfo *cfb = be->be_private;
5596         struct berval rdn;
5597         Entry *e, *parent;
5598         CfEntryInfo *ce, *ceparent;
5599         int i, unsupp = 0;
5600         BackendInfo *bi;
5601         ConfigArgs c;
5602         Connection conn = {0};
5603         OperationBuffer opbuf;
5604         Operation *op;
5605         slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
5606         SlapReply rs = {REP_RESULT};
5607         void *thrctx = NULL;
5608
5609         Debug( LDAP_DEBUG_TRACE, "config_back_db_open\n", 0, 0, 0);
5610
5611         /* If we have no explicitly configured ACLs, don't just use
5612          * the global ACLs. Explicitly deny access to everything.
5613          */
5614         if ( frontendDB->be_acl && be->be_acl == frontendDB->be_acl ) {
5615                 parse_acl(be, "config_back_db_open", 0, 6, (char **)defacl, 0 );
5616         }
5617
5618         thrctx = ldap_pvt_thread_pool_context();
5619         connection_fake_init( &conn, &opbuf, thrctx );
5620         op = &opbuf.ob_op;
5621
5622         op->o_tag = LDAP_REQ_ADD;
5623         op->o_callback = &cb;
5624         op->o_bd = &cfb->cb_db;
5625         op->o_dn = op->o_bd->be_rootdn;
5626         op->o_ndn = op->o_bd->be_rootndn;
5627
5628         if ( !cfb->cb_use_ldif ) {
5629                 op->o_noop = 1;
5630         }
5631
5632         /* If we read the config from back-ldif, do some quick sanity checks */
5633         if ( cfb->cb_got_ldif ) {
5634                 return config_check_schema( op, cfb );
5635         }
5636
5637         /* create root of tree */
5638         rdn = config_rdn;
5639         c.private = cfb->cb_config;
5640         c.be = frontendDB;
5641         e = config_build_entry( op, &rs, NULL, &c, &rdn, &CFOC_GLOBAL, NULL );
5642         if ( !e ) {
5643                 return -1;
5644         }
5645         ce = e->e_private;
5646         cfb->cb_root = ce;
5647
5648         parent = e;
5649         ceparent = ce;
5650
5651 #ifdef SLAPD_MODULES
5652         /* Create Module nodes... */
5653         if ( modpaths.mp_loads ) {
5654                 if ( config_build_modules( &c, ceparent, op, &rs ) ){
5655                         return -1;
5656                 }
5657         }
5658 #endif
5659
5660         /* Create schema nodes... cn=schema will contain the hardcoded core
5661          * schema, read-only. Child objects will contain runtime loaded schema
5662          * files.
5663          */
5664         rdn = schema_rdn;
5665         c.private = NULL;
5666         e = config_build_entry( op, &rs, ceparent, &c, &rdn, &CFOC_SCHEMA, NULL );
5667         if ( !e ) {
5668                 return -1;
5669         }
5670         ce = e->e_private;
5671         ce->ce_private = cfb->cb_config;
5672         cf_at_tail = at_sys_tail;
5673         cf_oc_tail = oc_sys_tail;
5674         cf_om_tail = om_sys_tail;
5675
5676         /* Create schema nodes for included schema... */
5677         if ( cfb->cb_config->c_kids ) {
5678                 c.depth = 0;
5679                 c.private = cfb->cb_config->c_kids;
5680                 if (config_build_schema_inc( &c, ce, op, &rs )) {
5681                         return -1;
5682                 }
5683         }
5684
5685         /* Create backend nodes. Skip if they don't provide a cf_table.
5686          * There usually aren't any of these.
5687          */
5688         
5689         c.line = 0;
5690         LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next) {
5691                 if (!bi->bi_cf_ocs) {
5692                         /* If it only supports the old config mech, complain. */
5693                         if ( bi->bi_config ) {
5694                                 Debug( LDAP_DEBUG_ANY,
5695                                         "WARNING: No dynamic config support for backend %s.\n",
5696                                         bi->bi_type, 0, 0 );
5697                                 unsupp++;
5698                         }
5699                         continue;
5700                 }
5701                 if (!bi->bi_private) continue;
5702
5703                 rdn.bv_val = c.log;
5704                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( c.log ),
5705                         "%s=%s", cfAd_backend->ad_cname.bv_val, bi->bi_type);
5706                 if ( rdn.bv_len >= sizeof( c.log ) ) {
5707                         /* FIXME: holler ... */ ;
5708                 }
5709                 c.bi = bi;
5710                 e = config_build_entry( op, &rs, ceparent, &c, &rdn, &CFOC_BACKEND,
5711                         bi->bi_cf_ocs );
5712                 if ( !e ) {
5713                         return -1;
5714                 }
5715         }
5716
5717         /* Create database nodes... */
5718         frontendDB->be_cf_ocs = &CFOC_FRONTEND;
5719         LDAP_STAILQ_NEXT(frontendDB, be_next) = LDAP_STAILQ_FIRST(&backendDB);
5720         for ( i = -1, be = frontendDB ; be;
5721                 i++, be = LDAP_STAILQ_NEXT( be, be_next )) {
5722                 slap_overinfo *oi = NULL;
5723
5724                 if ( overlay_is_over( be )) {
5725                         oi = be->bd_info->bi_private;
5726                         bi = oi->oi_orig;
5727                 } else {
5728                         bi = be->bd_info;
5729                 }
5730
5731                 /* If this backend supports the old config mechanism, but not
5732                  * the new mech, complain.
5733                  */
5734                 if ( !be->be_cf_ocs && bi->bi_db_config ) {
5735                         Debug( LDAP_DEBUG_ANY,
5736                                 "WARNING: No dynamic config support for database %s.\n",
5737                                 bi->bi_type, 0, 0 );
5738                         unsupp++;
5739                 }
5740                 rdn.bv_val = c.log;
5741                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( c.log ),
5742                         "%s=" SLAP_X_ORDERED_FMT "%s", cfAd_database->ad_cname.bv_val,
5743                         i, bi->bi_type);
5744                 if ( rdn.bv_len >= sizeof( c.log ) ) {
5745                         /* FIXME: holler ... */ ;
5746                 }
5747                 c.be = be;
5748                 c.bi = bi;
5749                 e = config_build_entry( op, &rs, ceparent, &c, &rdn, &CFOC_DATABASE,
5750                         be->be_cf_ocs );
5751                 if ( !e ) {
5752                         return -1;
5753                 }
5754                 ce = e->e_private;
5755                 if ( be->be_cf_ocs && be->be_cf_ocs->co_cfadd )
5756                         be->be_cf_ocs->co_cfadd( op, &rs, e, &c );
5757                 /* Iterate through overlays */
5758                 if ( oi ) {
5759                         slap_overinst *on;
5760                         Entry *oe;
5761                         int j;
5762
5763                         for (j=0,on=oi->oi_list; on; j++,on=on->on_next) {
5764                                 if ( on->on_bi.bi_db_config && !on->on_bi.bi_cf_ocs ) {
5765                                         Debug( LDAP_DEBUG_ANY,
5766                                                 "WARNING: No dynamic config support for overlay %s.\n",
5767                                                 on->on_bi.bi_type, 0, 0 );
5768                                         unsupp++;
5769                                 }
5770                                 rdn.bv_val = c.log;
5771                                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( c.log ),
5772                                         "%s=" SLAP_X_ORDERED_FMT "%s",
5773                                         cfAd_overlay->ad_cname.bv_val, j, on->on_bi.bi_type );
5774                                 if ( rdn.bv_len >= sizeof( c.log ) ) {
5775                                         /* FIXME: holler ... */ ;
5776                                 }
5777                                 c.be = be;
5778                                 c.bi = &on->on_bi;
5779                                 oe = config_build_entry( op, &rs, ce, &c, &rdn,
5780                                         &CFOC_OVERLAY, c.bi->bi_cf_ocs );
5781                                 if ( !oe ) {
5782                                         return -1;
5783                                 }
5784                                 if ( c.bi->bi_cf_ocs && c.bi->bi_cf_ocs->co_cfadd )
5785                                         c.bi->bi_cf_ocs->co_cfadd( op, &rs, oe, &c );
5786                         }
5787                 }
5788         }
5789         if ( thrctx )
5790                 ldap_pvt_thread_pool_context_reset( thrctx );
5791
5792         if ( unsupp  && cfb->cb_use_ldif ) {
5793                 Debug( LDAP_DEBUG_ANY, "\nWARNING: The converted cn=config "
5794                         "directory is incomplete and may not work.\n\n", 0, 0, 0 );
5795         }
5796
5797         return 0;
5798 }
5799
5800 static void
5801 cfb_free_cffile( ConfigFile *cf )
5802 {
5803         ConfigFile *next;
5804
5805         for (; cf; cf=next) {
5806                 next = cf->c_sibs;
5807                 if ( cf->c_kids )
5808                         cfb_free_cffile( cf->c_kids );
5809                 ch_free( cf->c_file.bv_val );
5810                 ber_bvarray_free( cf->c_dseFiles );
5811                 ch_free( cf );
5812         }
5813 }
5814
5815 static void
5816 cfb_free_entries( CfEntryInfo *ce )
5817 {
5818         CfEntryInfo *next;
5819
5820         for (; ce; ce=next) {
5821                 next = ce->ce_sibs;
5822                 if ( ce->ce_kids )
5823                         cfb_free_entries( ce->ce_kids );
5824                 ce->ce_entry->e_private = NULL;
5825                 entry_free( ce->ce_entry );
5826                 ch_free( ce );
5827         }
5828 }
5829
5830 static int
5831 config_back_db_close( BackendDB *be, ConfigReply *cr )
5832 {
5833         CfBackInfo *cfb = be->be_private;
5834
5835         cfb_free_entries( cfb->cb_root );
5836         cfb->cb_root = NULL;
5837
5838         if ( cfb->cb_db.bd_info ) {
5839                 backend_shutdown( &cfb->cb_db );
5840         }
5841
5842         return 0;
5843 }
5844
5845 static int
5846 config_back_db_destroy( BackendDB *be, ConfigReply *cr )
5847 {
5848         CfBackInfo *cfb = be->be_private;
5849
5850         cfb_free_cffile( cfb->cb_config );
5851
5852         ch_free( cfdir.bv_val );
5853
5854         avl_free( CfOcTree, NULL );
5855
5856         if ( cfb->cb_db.bd_info ) {
5857                 cfb->cb_db.be_suffix = NULL;
5858                 cfb->cb_db.be_nsuffix = NULL;
5859                 BER_BVZERO( &cfb->cb_db.be_rootdn );
5860                 BER_BVZERO( &cfb->cb_db.be_rootndn );
5861
5862                 backend_destroy_one( &cfb->cb_db, 0 );
5863         }
5864
5865         loglevel_destroy();
5866
5867         return 0;
5868 }
5869
5870 static int
5871 config_back_db_init( BackendDB *be, ConfigReply* cr )
5872 {
5873         struct berval dn;
5874         CfBackInfo *cfb;
5875
5876         cfb = &cfBackInfo;
5877         cfb->cb_config = ch_calloc( 1, sizeof(ConfigFile));
5878         cfn = cfb->cb_config;
5879         be->be_private = cfb;
5880
5881         ber_dupbv( &be->be_rootdn, &config_rdn );
5882         ber_dupbv( &be->be_rootndn, &be->be_rootdn );
5883         ber_dupbv( &dn, &be->be_rootdn );
5884         ber_bvarray_add( &be->be_suffix, &dn );
5885         ber_dupbv( &dn, &be->be_rootdn );
5886         ber_bvarray_add( &be->be_nsuffix, &dn );
5887
5888         /* Hide from namingContexts */
5889         SLAP_BFLAGS(be) |= SLAP_BFLAG_CONFIG;
5890
5891         return 0;
5892 }
5893
5894 static int
5895 config_back_destroy( BackendInfo *bi )
5896 {
5897         ldif_must_b64_encode_release();
5898         return 0;
5899 }
5900
5901 static int
5902 config_tool_entry_open( BackendDB *be, int mode )
5903 {
5904         CfBackInfo *cfb = be->be_private;
5905         BackendInfo *bi = cfb->cb_db.bd_info;
5906
5907         if ( bi && bi->bi_tool_entry_open )
5908                 return bi->bi_tool_entry_open( &cfb->cb_db, mode );
5909         else
5910                 return -1;
5911         
5912 }
5913
5914 static int
5915 config_tool_entry_close( BackendDB *be )
5916 {
5917         CfBackInfo *cfb = be->be_private;
5918         BackendInfo *bi = cfb->cb_db.bd_info;
5919
5920         if ( bi && bi->bi_tool_entry_close )
5921                 return bi->bi_tool_entry_close( &cfb->cb_db );
5922         else
5923                 return -1;
5924 }
5925
5926 static ID
5927 config_tool_entry_first( BackendDB *be )
5928 {
5929         CfBackInfo *cfb = be->be_private;
5930         BackendInfo *bi = cfb->cb_db.bd_info;
5931
5932         if ( bi && bi->bi_tool_entry_first )
5933                 return bi->bi_tool_entry_first( &cfb->cb_db );
5934         else
5935                 return NOID;
5936 }
5937
5938 static ID
5939 config_tool_entry_next( BackendDB *be )
5940 {
5941         CfBackInfo *cfb = be->be_private;
5942         BackendInfo *bi = cfb->cb_db.bd_info;
5943
5944         if ( bi && bi->bi_tool_entry_next )
5945                 return bi->bi_tool_entry_next( &cfb->cb_db );
5946         else
5947                 return NOID;
5948 }
5949
5950 static Entry *
5951 config_tool_entry_get( BackendDB *be, ID id )
5952 {
5953         CfBackInfo *cfb = be->be_private;
5954         BackendInfo *bi = cfb->cb_db.bd_info;
5955
5956         if ( bi && bi->bi_tool_entry_get )
5957                 return bi->bi_tool_entry_get( &cfb->cb_db, id );
5958         else
5959                 return NULL;
5960 }
5961
5962 static int entry_put_got_frontend=0;
5963 static int entry_put_got_config=0;
5964 static ID
5965 config_tool_entry_put( BackendDB *be, Entry *e, struct berval *text )
5966 {
5967         CfBackInfo *cfb = be->be_private;
5968         BackendInfo *bi = cfb->cb_db.bd_info;
5969         int rc;
5970         struct berval rdn, vals[ 2 ];
5971         ConfigArgs ca;
5972         OperationBuffer opbuf;
5973         Entry *ce;
5974         Connection conn = {0};
5975         Operation *op = NULL;
5976         void *thrctx;
5977         int isFrontend = 0;
5978
5979         /* Create entry for frontend database if it does not exist already */
5980         if ( !entry_put_got_frontend ) {
5981                 if ( !strncmp( e->e_nname.bv_val, "olcDatabase", 
5982                                 STRLENOF( "olcDatabase" ))) {
5983                         if ( strncmp( e->e_nname.bv_val + 
5984                                         STRLENOF( "olcDatabase" ), "={-1}frontend",
5985                                         STRLENOF( "={-1}frontend" )) && 
5986                                         strncmp( e->e_nname.bv_val + 
5987                                         STRLENOF( "olcDatabase" ), "=frontend",
5988                                         STRLENOF( "=frontend" ))) {
5989                                 vals[1].bv_len = 0;
5990                                 vals[1].bv_val = NULL;
5991                                 memset( &ca, 0, sizeof(ConfigArgs));
5992                                 ca.be = frontendDB;
5993                                 ca.bi = frontendDB->bd_info;
5994                                 ca.be->be_cf_ocs = &CFOC_FRONTEND;
5995                                 rdn.bv_val = ca.log;
5996                                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( ca.log ),
5997                                         "%s=" SLAP_X_ORDERED_FMT "%s",
5998                                         cfAd_database->ad_cname.bv_val, -1,
5999                                         ca.bi->bi_type);
6000                                 ce = config_build_entry( NULL, NULL, cfb->cb_root, &ca, &rdn,
6001                                                 &CFOC_DATABASE, ca.be->be_cf_ocs );
6002                                 thrctx = ldap_pvt_thread_pool_context();
6003                                 connection_fake_init2( &conn, &opbuf, thrctx,0 );
6004                                 op = &opbuf.ob_op;
6005                                 op->o_bd = &cfb->cb_db;
6006                                 op->o_tag = LDAP_REQ_ADD;
6007                                 op->ora_e = ce;
6008                                 op->o_dn = be->be_rootdn;
6009                                 op->o_ndn = be->be_rootndn;
6010                                 rc = slap_add_opattrs(op, NULL, NULL, 0, 0);
6011                                 if ( rc != LDAP_SUCCESS ) {
6012                                         text->bv_val = "autocreation of \"olcDatabase={-1}frontend\" failed";
6013                                         text->bv_len = STRLENOF("autocreation of \"olcDatabase={-1}frontend\" failed");
6014                                         return NOID;
6015                                 }
6016
6017                                 if ( ce && bi && bi->bi_tool_entry_put && 
6018                                                 bi->bi_tool_entry_put( &cfb->cb_db, ce, text ) != NOID ) {
6019                                         entry_put_got_frontend++;
6020                                 } else {
6021                                         text->bv_val = "autocreation of \"olcDatabase={-1}frontend\" failed";
6022                                         text->bv_len = STRLENOF("autocreation of \"olcDatabase={-1}frontend\" failed");
6023                                         return NOID;
6024                                 }
6025                         } else {
6026                                 entry_put_got_frontend++;
6027                                 isFrontend = 1;
6028                         }
6029                 }
6030         }
6031         /* Create entry for config database if it does not exist already */
6032         if ( !entry_put_got_config && !isFrontend ) {
6033                 if ( !strncmp( e->e_nname.bv_val, "olcDatabase",
6034                                 STRLENOF( "olcDatabase" ))) {
6035                         if ( strncmp( e->e_nname.bv_val +
6036                                         STRLENOF( "olcDatabase" ), "={0}config",
6037                                         STRLENOF( "={0}config" )) &&
6038                                         strncmp( e->e_nname.bv_val +
6039                                         STRLENOF( "olcDatabase" ), "=config",
6040                                         STRLENOF( "=config" )) ) {
6041                                 vals[1].bv_len = 0;
6042                                 vals[1].bv_val = NULL;
6043                                 memset( &ca, 0, sizeof(ConfigArgs));
6044                                 ca.be = LDAP_STAILQ_FIRST( &backendDB );
6045                                 ca.bi = ca.be->bd_info;
6046                                 rdn.bv_val = ca.log;
6047                                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( ca.log ),
6048                                         "%s=" SLAP_X_ORDERED_FMT "%s",
6049                                         cfAd_database->ad_cname.bv_val, 0,
6050                                         ca.bi->bi_type);
6051                                 ce = config_build_entry( NULL, NULL, cfb->cb_root, &ca, &rdn, &CFOC_DATABASE,
6052                                                 ca.be->be_cf_ocs );
6053                                 if ( ! op ) {
6054                                         thrctx = ldap_pvt_thread_pool_context();
6055                                         connection_fake_init2( &conn, &opbuf, thrctx,0 );
6056                                         op = &opbuf.ob_op;
6057                                         op->o_bd = &cfb->cb_db;
6058                                         op->o_tag = LDAP_REQ_ADD;
6059                                         op->o_dn = be->be_rootdn;
6060                                         op->o_ndn = be->be_rootndn;
6061                                 }
6062                                 op->ora_e = ce;
6063                                 rc = slap_add_opattrs(op, NULL, NULL, 0, 0);
6064                                 if ( rc != LDAP_SUCCESS ) {
6065                                         text->bv_val = "autocreation of \"olcDatabase={0}config\" failed";
6066                                         text->bv_len = STRLENOF("autocreation of \"olcDatabase={0}config\" failed");
6067                                         return NOID;
6068                                 }
6069                                 if (ce && bi && bi->bi_tool_entry_put &&
6070                                                 bi->bi_tool_entry_put( &cfb->cb_db, ce, text ) != NOID ) {
6071                                         entry_put_got_config++;
6072                                 } else {
6073                                         text->bv_val = "autocreation of \"olcDatabase={0}config\" failed";
6074                                         text->bv_len = STRLENOF("autocreation of \"olcDatabase={0}config\" failed");
6075                                         return NOID;
6076                                 }
6077                         } else {
6078                                 entry_put_got_config++;
6079                         }
6080                 }
6081         }
6082         if ( bi && bi->bi_tool_entry_put &&
6083                 config_add_internal( cfb, e, &ca, NULL, NULL, NULL ) == 0 )
6084                 return bi->bi_tool_entry_put( &cfb->cb_db, e, text );
6085         else
6086                 return NOID;
6087 }
6088
6089 static struct {
6090         char *name;
6091         AttributeDescription **desc;
6092 } ads[] = {
6093         { "attribute", &cfAd_attr },
6094         { "backend", &cfAd_backend },
6095         { "database", &cfAd_database },
6096         { "include", &cfAd_include },
6097         { "objectclass", &cfAd_oc },
6098         { "objectidentifier", &cfAd_om },
6099         { "overlay", &cfAd_overlay },
6100         { NULL, NULL }
6101 };
6102
6103 /* Notes:
6104  *   add / delete: all types that may be added or deleted must use an
6105  * X-ORDERED attributeType for their RDN. Adding and deleting entries
6106  * should automatically renumber the index of any siblings as needed,
6107  * so that no gaps in the numbering sequence exist after the add/delete
6108  * is completed.
6109  *   What can be added:
6110  *     schema objects
6111  *     backend objects for backend-specific config directives
6112  *     database objects
6113  *     overlay objects
6114  *
6115  *   delete: probably no support this time around.
6116  *
6117  *   modrdn: generally not done. Will be invoked automatically by add/
6118  * delete to update numbering sequence. Perform as an explicit operation
6119  * so that the renumbering effect may be replicated. Subtree rename must
6120  * be supported, since renumbering a database will affect all its child
6121  * overlays.
6122  *
6123  *  modify: must be fully supported. 
6124  */
6125
6126 int
6127 config_back_initialize( BackendInfo *bi )
6128 {
6129         ConfigTable             *ct = config_back_cf_table;
6130         ConfigArgs ca;
6131         char                    *argv[4];
6132         int                     i;
6133         AttributeDescription    *ad = NULL;
6134         const char              *text;
6135         static char             *controls[] = {
6136                 LDAP_CONTROL_MANAGEDSAIT,
6137                 NULL
6138         };
6139
6140         /* Make sure we don't exceed the bits reserved for userland */
6141         config_check_userland( CFG_LAST );
6142
6143         bi->bi_controls = controls;
6144
6145         bi->bi_open = 0;
6146         bi->bi_close = 0;
6147         bi->bi_config = 0;
6148         bi->bi_destroy = config_back_destroy;
6149
6150         bi->bi_db_init = config_back_db_init;
6151         bi->bi_db_config = 0;
6152         bi->bi_db_open = config_back_db_open;
6153         bi->bi_db_close = config_back_db_close;
6154         bi->bi_db_destroy = config_back_db_destroy;
6155
6156         bi->bi_op_bind = config_back_bind;
6157         bi->bi_op_unbind = 0;
6158         bi->bi_op_search = config_back_search;
6159         bi->bi_op_compare = 0;
6160         bi->bi_op_modify = config_back_modify;
6161         bi->bi_op_modrdn = config_back_modrdn;
6162         bi->bi_op_add = config_back_add;
6163         bi->bi_op_delete = config_back_delete;
6164         bi->bi_op_abandon = 0;
6165
6166         bi->bi_extended = 0;
6167
6168         bi->bi_chk_referrals = 0;
6169
6170         bi->bi_access_allowed = slap_access_allowed;
6171
6172         bi->bi_connection_init = 0;
6173         bi->bi_connection_destroy = 0;
6174
6175         bi->bi_entry_release_rw = config_entry_release;
6176         bi->bi_entry_get_rw = config_back_entry_get;
6177
6178         bi->bi_tool_entry_open = config_tool_entry_open;
6179         bi->bi_tool_entry_close = config_tool_entry_close;
6180         bi->bi_tool_entry_first = config_tool_entry_first;
6181         bi->bi_tool_entry_next = config_tool_entry_next;
6182         bi->bi_tool_entry_get = config_tool_entry_get;
6183         bi->bi_tool_entry_put = config_tool_entry_put;
6184
6185         ca.argv = argv;
6186         argv[ 0 ] = "slapd";
6187         ca.argv = argv;
6188         ca.argc = 3;
6189         ca.fname = argv[0];
6190
6191         argv[3] = NULL;
6192         for (i=0; OidMacros[i].name; i++ ) {
6193                 argv[1] = OidMacros[i].name;
6194                 argv[2] = OidMacros[i].oid;
6195                 parse_oidm( &ca, 0, NULL );
6196         }
6197
6198         bi->bi_cf_ocs = cf_ocs;
6199
6200         i = config_register_schema( ct, cf_ocs );
6201         if ( i ) return i;
6202
6203         /* setup olcRootPW to be base64-encoded when written in LDIF form;
6204          * basically, we don't care if it fails */
6205         i = slap_str2ad( "olcRootPW", &ad, &text );
6206         if ( i ) {
6207                 Debug( LDAP_DEBUG_ANY, "config_back_initialize: "
6208                         "warning, unable to get \"olcRootPW\" "
6209                         "attribute description: %d: %s\n",
6210                         i, text, 0 );
6211         } else {
6212                 (void)ldif_must_b64_encode_register( ad->ad_cname.bv_val,
6213                         ad->ad_type->sat_oid );
6214         }
6215
6216         /* set up the notable AttributeDescriptions */
6217         i = 0;
6218         for (;ct->name;ct++) {
6219                 if (strcmp(ct->name, ads[i].name)) continue;
6220                 *ads[i].desc = ct->ad;
6221                 i++;
6222                 if (!ads[i].name) break;
6223         }
6224
6225         return 0;
6226 }
6227