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