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