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