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