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