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