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