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