]> git.sur5r.net Git - openldap/blob - servers/slapd/bconfig.c
declaration after code (introduced by ITS#6428 fix)
[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 or values from frontend */
2212                 if ( c->be == frontendDB ) {
2213                         lim->lms_s_soft = SLAPD_DEFAULT_SIZELIMIT;
2214                         lim->lms_s_hard = 0;
2215                         lim->lms_s_unchecked = -1;
2216                         lim->lms_s_pr = 0;
2217                         lim->lms_s_pr_hide = 0;
2218                         lim->lms_s_pr_total = 0;
2219                 } else {
2220                         lim->lms_s_soft = frontendDB->be_def_limit.lms_s_soft;
2221                         lim->lms_s_hard = frontendDB->be_def_limit.lms_s_hard;
2222                         lim->lms_s_unchecked = frontendDB->be_def_limit.lms_s_unchecked;
2223                         lim->lms_s_pr = frontendDB->be_def_limit.lms_s_pr;
2224                         lim->lms_s_pr_hide = frontendDB->be_def_limit.lms_s_pr_hide;
2225                         lim->lms_s_pr_total = frontendDB->be_def_limit.lms_s_pr_total;
2226                 }
2227                 goto ok;
2228         }
2229         for(i = 1; i < c->argc; i++) {
2230                 if(!strncasecmp(c->argv[i], "size", 4)) {
2231                         rc = limits_parse_one(c->argv[i], lim);
2232                         if ( rc ) {
2233                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to parse value", c->argv[0] );
2234                                 Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2235                                         c->log, c->cr_msg, c->argv[i]);
2236                                 return(1);
2237                         }
2238                 } else {
2239                         if(!strcasecmp(c->argv[i], "unlimited")) {
2240                                 lim->lms_s_soft = -1;
2241                         } else {
2242                                 if ( lutil_atoix( &lim->lms_s_soft, c->argv[i], 0 ) != 0 ) {
2243                                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to parse limit", c->argv[0]);
2244                                         Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2245                                                 c->log, c->cr_msg, c->argv[i]);
2246                                         return(1);
2247                                 }
2248                         }
2249                         lim->lms_s_hard = 0;
2250                 }
2251         }
2252
2253 ok:
2254         if ( ( c->be == frontendDB ) && ( c->ca_entry ) ) {
2255                 /* This is a modification to the global limits apply it to
2256                  * the other databases as needed */
2257                 AttributeDescription *ad=NULL;
2258                 const char *text = NULL;
2259                 CfEntryInfo *ce = c->ca_entry->e_private;
2260
2261                 slap_str2ad(c->argv[0], &ad, &text);
2262                 /* if we got here... */
2263                 assert( ad != NULL );
2264
2265                 if ( ce->ce_type == Cft_Global ){
2266                         ce = ce->ce_kids;
2267                 }
2268                 for (; ce; ce=ce->ce_sibs) {
2269                         Entry *dbe = ce->ce_entry;
2270                         if ( (ce->ce_type == Cft_Database) && (ce->ce_be != frontendDB)
2271                                         && (!attr_find(dbe->e_attrs, ad)) ) {
2272                                 ce->ce_be->be_def_limit.lms_s_soft = lim->lms_s_soft;
2273                                 ce->ce_be->be_def_limit.lms_s_hard = lim->lms_s_hard;
2274                                 ce->ce_be->be_def_limit.lms_s_unchecked =lim->lms_s_unchecked;
2275                                 ce->ce_be->be_def_limit.lms_s_pr =lim->lms_s_pr;
2276                                 ce->ce_be->be_def_limit.lms_s_pr_hide =lim->lms_s_pr_hide;
2277                                 ce->ce_be->be_def_limit.lms_s_pr_total =lim->lms_s_pr_total;
2278                         }
2279                 }
2280         }
2281         return(0);
2282 }
2283
2284 static int
2285 config_timelimit(ConfigArgs *c) {
2286         int i, rc = 0;
2287         struct slap_limits_set *lim = &c->be->be_def_limit;
2288         if (c->op == SLAP_CONFIG_EMIT) {
2289                 char buf[8192];
2290                 struct berval bv;
2291                 bv.bv_val = buf;
2292                 bv.bv_len = 0;
2293                 limits_unparse_one( lim, SLAP_LIMIT_TIME, &bv, sizeof( buf ) );
2294                 if ( !BER_BVISEMPTY( &bv ))
2295                         value_add_one( &c->rvalue_vals, &bv );
2296                 else
2297                         rc = 1;
2298                 return rc;
2299         } else if ( c->op == LDAP_MOD_DELETE ) {
2300                 /* Reset to defaults or values from frontend */
2301                 if ( c->be == frontendDB ) {
2302                         lim->lms_t_soft = SLAPD_DEFAULT_TIMELIMIT;
2303                         lim->lms_t_hard = 0;
2304                 } else {
2305                         lim->lms_t_soft = frontendDB->be_def_limit.lms_t_soft;
2306                         lim->lms_t_hard = frontendDB->be_def_limit.lms_t_hard;
2307                 }
2308                 goto ok;
2309         }
2310         for(i = 1; i < c->argc; i++) {
2311                 if(!strncasecmp(c->argv[i], "time", 4)) {
2312                         rc = limits_parse_one(c->argv[i], lim);
2313                         if ( rc ) {
2314                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to parse value", c->argv[0] );
2315                                 Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2316                                         c->log, c->cr_msg, c->argv[i]);
2317                                 return(1);
2318                         }
2319                 } else {
2320                         if(!strcasecmp(c->argv[i], "unlimited")) {
2321                                 lim->lms_t_soft = -1;
2322                         } else {
2323                                 if ( lutil_atoix( &lim->lms_t_soft, c->argv[i], 0 ) != 0 ) {
2324                                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to parse limit", c->argv[0]);
2325                                         Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2326                                                 c->log, c->cr_msg, c->argv[i]);
2327                                         return(1);
2328                                 }
2329                         }
2330                         lim->lms_t_hard = 0;
2331                 }
2332         }
2333
2334 ok:
2335         if ( ( c->be == frontendDB ) && ( c->ca_entry ) ) {
2336                 /* This is a modification to the global limits apply it to
2337                  * the other databases as needed */
2338                 AttributeDescription *ad=NULL;
2339                 const char *text = NULL;
2340                 CfEntryInfo *ce = c->ca_entry->e_private;
2341
2342                 slap_str2ad(c->argv[0], &ad, &text);
2343                 /* if we got here... */
2344                 assert( ad != NULL );
2345
2346                 if ( ce->ce_type == Cft_Global ){
2347                         ce = ce->ce_kids;
2348                 }
2349                 for (; ce; ce=ce->ce_sibs) {
2350                         Entry *dbe = ce->ce_entry;
2351                         if ( (ce->ce_type == Cft_Database) && (ce->ce_be != frontendDB)
2352                                         && (!attr_find(dbe->e_attrs, ad)) ) {
2353                                 ce->ce_be->be_def_limit.lms_t_soft = lim->lms_t_soft;
2354                                 ce->ce_be->be_def_limit.lms_t_hard = lim->lms_t_hard;
2355                         }
2356                 }
2357         }
2358         return(0);
2359 }
2360
2361 static int
2362 config_overlay(ConfigArgs *c) {
2363         if (c->op == SLAP_CONFIG_EMIT) {
2364                 return 1;
2365         } else if ( c->op == LDAP_MOD_DELETE ) {
2366                 assert(0);
2367         }
2368         if(c->argv[1][0] == '-' && overlay_config(c->be, &c->argv[1][1],
2369                 c->valx, &c->bi, &c->reply)) {
2370                 /* log error */
2371                 Debug( LDAP_DEBUG_ANY,
2372                         "%s: (optional) %s overlay \"%s\" configuration failed.\n",
2373                         c->log, c->be == frontendDB ? "global " : "", &c->argv[1][1]);
2374                 return 1;
2375         } else if(overlay_config(c->be, c->argv[1], c->valx, &c->bi, &c->reply)) {
2376                 return(1);
2377         }
2378         return(0);
2379 }
2380
2381 static int
2382 config_subordinate(ConfigArgs *c)
2383 {
2384         int rc = 1;
2385         int advertise = 0;
2386
2387         switch( c->op ) {
2388         case SLAP_CONFIG_EMIT:
2389                 if ( SLAP_GLUE_SUBORDINATE( c->be )) {
2390                         struct berval bv;
2391
2392                         bv.bv_val = SLAP_GLUE_ADVERTISE( c->be ) ? "advertise" : "TRUE";
2393                         bv.bv_len = SLAP_GLUE_ADVERTISE( c->be ) ? STRLENOF("advertise") :
2394                                 STRLENOF("TRUE");
2395
2396                         value_add_one( &c->rvalue_vals, &bv );
2397                         rc = 0;
2398                 }
2399                 break;
2400         case LDAP_MOD_DELETE:
2401                 if ( !c->line  || strcasecmp( c->line, "advertise" )) {
2402                         glue_sub_del( c->be );
2403                 } else {
2404                         SLAP_DBFLAGS( c->be ) &= ~SLAP_DBFLAG_GLUE_ADVERTISE;
2405                 }
2406                 rc = 0;
2407                 break;
2408         case LDAP_MOD_ADD:
2409         case SLAP_CONFIG_ADD:
2410                 if ( c->be->be_nsuffix == NULL ) {
2411                         /* log error */
2412                         snprintf( c->cr_msg, sizeof( c->cr_msg),
2413                                 "subordinate configuration needs a suffix" );
2414                         Debug( LDAP_DEBUG_ANY,
2415                                 "%s: %s.\n",
2416                                 c->log, c->cr_msg, 0 );
2417                         rc = 1;
2418                         break;
2419                 }
2420
2421                 if ( c->argc == 2 ) {
2422                         if ( strcasecmp( c->argv[1], "advertise" ) == 0 ) {
2423                                 advertise = 1;
2424
2425                         } else if ( strcasecmp( c->argv[1], "TRUE" ) != 0 ) {
2426                                 /* log error */
2427                                 snprintf( c->cr_msg, sizeof( c->cr_msg),
2428                                         "subordinate must be \"TRUE\" or \"advertise\"" );
2429                                 Debug( LDAP_DEBUG_ANY,
2430                                         "%s: suffix \"%s\": %s.\n",
2431                                         c->log, c->be->be_suffix[0].bv_val, c->cr_msg );
2432                                 rc = 1;
2433                                 break;
2434                         }
2435                 }
2436
2437                 rc = glue_sub_add( c->be, advertise, CONFIG_ONLINE_ADD( c ));
2438                 break;
2439         }
2440
2441         return rc;
2442 }
2443
2444 /*
2445  * [listener=<listener>] [{read|write}=]<size>
2446  */
2447
2448 #ifdef LDAP_TCP_BUFFER
2449 static BerVarray tcp_buffer;
2450 int tcp_buffer_num;
2451
2452 #define SLAP_TCP_RMEM (0x1U)
2453 #define SLAP_TCP_WMEM (0x2U)
2454
2455 static int
2456 tcp_buffer_parse( struct berval *val, int argc, char **argv,
2457                 int *size, int *rw, Listener **l )
2458 {
2459         int i, rc = LDAP_SUCCESS;
2460         LDAPURLDesc *lud = NULL;
2461         char *ptr;
2462
2463         if ( val != NULL && argv == NULL ) {
2464                 char *s = val->bv_val;
2465
2466                 argv = ldap_str2charray( s, " \t" );
2467                 if ( argv == NULL ) {
2468                         return LDAP_OTHER;
2469                 }
2470         }
2471
2472         i = 0;
2473         if ( strncasecmp( argv[ i ], "listener=", STRLENOF( "listener=" ) )
2474                 == 0 )
2475         {
2476                 char *url = argv[ i ] + STRLENOF( "listener=" );
2477                 
2478                 if ( ldap_url_parse( url, &lud ) ) {
2479                         rc = LDAP_INVALID_SYNTAX;
2480                         goto done;
2481                 }
2482
2483                 *l = config_check_my_url( url, lud );
2484                 if ( *l == NULL ) {
2485                         rc = LDAP_NO_SUCH_ATTRIBUTE;
2486                         goto done;
2487                 }
2488
2489                 i++;
2490         }
2491
2492         ptr = argv[ i ];
2493         if ( strncasecmp( ptr, "read=", STRLENOF( "read=" ) ) == 0 ) {
2494                 *rw |= SLAP_TCP_RMEM;
2495                 ptr += STRLENOF( "read=" );
2496
2497         } else if ( strncasecmp( ptr, "write=", STRLENOF( "write=" ) ) == 0 ) {
2498                 *rw |= SLAP_TCP_WMEM;
2499                 ptr += STRLENOF( "write=" );
2500
2501         } else {
2502                 *rw |= ( SLAP_TCP_RMEM | SLAP_TCP_WMEM );
2503         }
2504
2505         /* accept any base */
2506         if ( lutil_atoix( size, ptr, 0 ) ) {
2507                 rc = LDAP_INVALID_SYNTAX;
2508                 goto done;
2509         }
2510
2511 done:;
2512         if ( val != NULL && argv != NULL ) {
2513                 ldap_charray_free( argv );
2514         }
2515
2516         if ( lud != NULL ) {
2517                 ldap_free_urldesc( lud );
2518         }
2519
2520         return rc;
2521 }
2522
2523 static int
2524 tcp_buffer_delete_one( struct berval *val )
2525 {
2526         int rc = 0;
2527         int size = -1, rw = 0;
2528         Listener *l = NULL;
2529
2530         rc = tcp_buffer_parse( val, 0, NULL, &size, &rw, &l );
2531         if ( rc != 0 ) {
2532                 return rc;
2533         }
2534
2535         if ( l != NULL ) {
2536                 int i;
2537                 Listener **ll = slapd_get_listeners();
2538
2539                 for ( i = 0; ll[ i ] != NULL; i++ ) {
2540                         if ( ll[ i ] == l ) break;
2541                 }
2542
2543                 if ( ll[ i ] == NULL ) {
2544                         return LDAP_NO_SUCH_ATTRIBUTE;
2545                 }
2546
2547                 if ( rw & SLAP_TCP_RMEM ) l->sl_tcp_rmem = -1;
2548                 if ( rw & SLAP_TCP_WMEM ) l->sl_tcp_wmem = -1;
2549
2550                 for ( i++ ; ll[ i ] != NULL && bvmatch( &l->sl_url, &ll[ i ]->sl_url ); i++ ) {
2551                         if ( rw & SLAP_TCP_RMEM ) ll[ i ]->sl_tcp_rmem = -1;
2552                         if ( rw & SLAP_TCP_WMEM ) ll[ i ]->sl_tcp_wmem = -1;
2553                 }
2554
2555         } else {
2556                 /* NOTE: this affects listeners without a specific setting,
2557                  * does not reset all listeners.  If a listener without
2558                  * specific settings was assigned a buffer because of
2559                  * a global setting, it will not be reset.  In any case,
2560                  * buffer changes will only take place at restart. */
2561                 if ( rw & SLAP_TCP_RMEM ) slapd_tcp_rmem = -1;
2562                 if ( rw & SLAP_TCP_WMEM ) slapd_tcp_wmem = -1;
2563         }
2564
2565         return rc;
2566 }
2567
2568 static int
2569 tcp_buffer_delete( BerVarray vals )
2570 {
2571         int i;
2572
2573         for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
2574                 tcp_buffer_delete_one( &vals[ i ] );
2575         }
2576
2577         return 0;
2578 }
2579
2580 static int
2581 tcp_buffer_unparse( int size, int rw, Listener *l, struct berval *val )
2582 {
2583         char buf[sizeof("2147483648")], *ptr;
2584
2585         /* unparse for later use */
2586         val->bv_len = snprintf( buf, sizeof( buf ), "%d", size );
2587         if ( l != NULL ) {
2588                 val->bv_len += STRLENOF( "listener=" " " ) + l->sl_url.bv_len;
2589         }
2590
2591         if ( rw != ( SLAP_TCP_RMEM | SLAP_TCP_WMEM ) ) {
2592                 if ( rw & SLAP_TCP_RMEM ) {
2593                         val->bv_len += STRLENOF( "read=" );
2594                 } else if ( rw & SLAP_TCP_WMEM ) {
2595                         val->bv_len += STRLENOF( "write=" );
2596                 }
2597         }
2598
2599         val->bv_val = SLAP_MALLOC( val->bv_len + 1 );
2600
2601         ptr = val->bv_val;
2602
2603         if ( l != NULL ) {
2604                 ptr = lutil_strcopy( ptr, "listener=" );
2605                 ptr = lutil_strncopy( ptr, l->sl_url.bv_val, l->sl_url.bv_len );
2606                 *ptr++ = ' ';
2607         }
2608
2609         if ( rw != ( SLAP_TCP_RMEM | SLAP_TCP_WMEM ) ) {
2610                 if ( rw & SLAP_TCP_RMEM ) {
2611                         ptr = lutil_strcopy( ptr, "read=" );
2612                 } else if ( rw & SLAP_TCP_WMEM ) {
2613                         ptr = lutil_strcopy( ptr, "write=" );
2614                 }
2615         }
2616
2617         ptr = lutil_strcopy( ptr, buf );
2618         *ptr = '\0';
2619
2620         assert( val->bv_val + val->bv_len == ptr );
2621
2622         return LDAP_SUCCESS;
2623 }
2624
2625 static int
2626 tcp_buffer_add_one( int argc, char **argv )
2627 {
2628         int rc = 0;
2629         int size = -1, rw = 0;
2630         Listener *l = NULL;
2631
2632         struct berval val;
2633
2634         /* parse */
2635         rc = tcp_buffer_parse( NULL, argc, argv, &size, &rw, &l );
2636         if ( rc != 0 ) {
2637                 return rc;
2638         }
2639
2640         /* unparse for later use */
2641         rc = tcp_buffer_unparse( size, rw, l, &val );
2642         if ( rc != LDAP_SUCCESS ) {
2643                 return rc;
2644         }
2645
2646         /* use parsed values */
2647         if ( l != NULL ) {
2648                 int i;
2649                 Listener **ll = slapd_get_listeners();
2650
2651                 for ( i = 0; ll[ i ] != NULL; i++ ) {
2652                         if ( ll[ i ] == l ) break;
2653                 }
2654
2655                 if ( ll[ i ] == NULL ) {
2656                         return LDAP_NO_SUCH_ATTRIBUTE;
2657                 }
2658
2659                 /* buffer only applies to TCP listeners;
2660                  * we do not do any check here, and delegate them
2661                  * to setsockopt(2) */
2662                 if ( rw & SLAP_TCP_RMEM ) l->sl_tcp_rmem = size;
2663                 if ( rw & SLAP_TCP_WMEM ) l->sl_tcp_wmem = size;
2664
2665                 for ( i++ ; ll[ i ] != NULL && bvmatch( &l->sl_url, &ll[ i ]->sl_url ); i++ ) {
2666                         if ( rw & SLAP_TCP_RMEM ) ll[ i ]->sl_tcp_rmem = size;
2667                         if ( rw & SLAP_TCP_WMEM ) ll[ i ]->sl_tcp_wmem = size;
2668                 }
2669
2670         } else {
2671                 /* NOTE: this affects listeners without a specific setting,
2672                  * does not set all listeners */
2673                 if ( rw & SLAP_TCP_RMEM ) slapd_tcp_rmem = size;
2674                 if ( rw & SLAP_TCP_WMEM ) slapd_tcp_wmem = size;
2675         }
2676
2677         tcp_buffer = SLAP_REALLOC( tcp_buffer, sizeof( struct berval ) * ( tcp_buffer_num + 2 ) );
2678         /* append */
2679         tcp_buffer[ tcp_buffer_num ] = val;
2680
2681         tcp_buffer_num++;
2682         BER_BVZERO( &tcp_buffer[ tcp_buffer_num ] );
2683
2684         return rc;
2685 }
2686
2687 static int
2688 config_tcp_buffer( ConfigArgs *c )
2689 {
2690         if ( c->op == SLAP_CONFIG_EMIT ) {
2691                 if ( tcp_buffer == NULL || BER_BVISNULL( &tcp_buffer[ 0 ] ) ) {
2692                         return 1;
2693                 }
2694                 value_add( &c->rvalue_vals, tcp_buffer );
2695                 value_add( &c->rvalue_nvals, tcp_buffer );
2696                 
2697         } else if ( c->op == LDAP_MOD_DELETE ) {
2698                 if ( !c->line  ) {
2699                         tcp_buffer_delete( tcp_buffer );
2700                         ber_bvarray_free( tcp_buffer );
2701                         tcp_buffer = NULL;
2702                         tcp_buffer_num = 0;
2703
2704                 } else {
2705                         int rc = 0;
2706                         int size = -1, rw = 0;
2707                         Listener *l = NULL;
2708
2709                         struct berval val = BER_BVNULL;
2710
2711                         int i;
2712
2713                         if ( tcp_buffer_num == 0 ) {
2714                                 return 1;
2715                         }
2716
2717                         /* parse */
2718                         rc = tcp_buffer_parse( NULL, c->argc - 1, &c->argv[ 1 ], &size, &rw, &l );
2719                         if ( rc != 0 ) {
2720                                 return 1;
2721                         }
2722
2723                         /* unparse for later use */
2724                         rc = tcp_buffer_unparse( size, rw, l, &val );
2725                         if ( rc != LDAP_SUCCESS ) {
2726                                 return 1;
2727                         }
2728
2729                         for ( i = 0; !BER_BVISNULL( &tcp_buffer[ i ] ); i++ ) {
2730                                 if ( bvmatch( &tcp_buffer[ i ], &val ) ) {
2731                                         break;
2732                                 }
2733                         }
2734
2735                         if ( BER_BVISNULL( &tcp_buffer[ i ] ) ) {
2736                                 /* not found */
2737                                 rc = 1;
2738                                 goto done;
2739                         }
2740
2741                         tcp_buffer_delete_one( &tcp_buffer[ i ] );
2742                         ber_memfree( tcp_buffer[ i ].bv_val );
2743                         for ( ; i < tcp_buffer_num; i++ ) {
2744                                 tcp_buffer[ i ] = tcp_buffer[ i + 1 ];
2745                         }
2746                         tcp_buffer_num--;
2747
2748 done:;
2749                         if ( !BER_BVISNULL( &val ) ) {
2750                                 SLAP_FREE( val.bv_val );
2751                         }
2752         
2753                 }
2754
2755         } else {
2756                 int rc;
2757
2758                 rc = tcp_buffer_add_one( c->argc - 1, &c->argv[ 1 ] );
2759                 if ( rc ) {
2760                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
2761                                 "<%s> unable to add value #%d",
2762                                 c->argv[0], tcp_buffer_num );
2763                         Debug( LDAP_DEBUG_ANY, "%s: %s\n",
2764                                 c->log, c->cr_msg, 0 );
2765                         return 1;
2766                 }
2767         }
2768
2769         return 0;
2770 }
2771 #endif /* LDAP_TCP_BUFFER */
2772
2773 static int
2774 config_suffix(ConfigArgs *c)
2775 {
2776         Backend *tbe;
2777         struct berval pdn, ndn;
2778         char    *notallowed = NULL;
2779
2780         if ( c->be == frontendDB ) {
2781                 notallowed = "frontend";
2782
2783         } else if ( SLAP_MONITOR(c->be) ) {
2784                 notallowed = "monitor";
2785
2786         } else if ( SLAP_CONFIG(c->be) ) {
2787                 notallowed = "config";
2788         }
2789
2790         if ( notallowed != NULL ) {
2791                 char    buf[ SLAP_TEXT_BUFLEN ] = { '\0' };
2792
2793                 switch ( c->op ) {
2794                 case LDAP_MOD_ADD:
2795                 case LDAP_MOD_DELETE:
2796                 case LDAP_MOD_REPLACE:
2797                 case LDAP_MOD_INCREMENT:
2798                 case SLAP_CONFIG_ADD:
2799                         if ( !BER_BVISNULL( &c->value_dn ) ) {
2800                                 snprintf( buf, sizeof( buf ), "<%s> ",
2801                                                 c->value_dn.bv_val );
2802                         }
2803
2804                         Debug(LDAP_DEBUG_ANY,
2805                                 "%s: suffix %snot allowed in %s database.\n",
2806                                 c->log, buf, notallowed );
2807                         break;
2808
2809                 case SLAP_CONFIG_EMIT:
2810                         /* don't complain when emitting... */
2811                         break;
2812
2813                 default:
2814                         /* FIXME: don't know what values may be valid;
2815                          * please remove assertion, or add legal values
2816                          * to either block */
2817                         assert( 0 );
2818                         break;
2819                 }
2820
2821                 return 1;
2822         }
2823
2824         if (c->op == SLAP_CONFIG_EMIT) {
2825                 if ( c->be->be_suffix == NULL
2826                                 || BER_BVISNULL( &c->be->be_suffix[0] ) )
2827                 {
2828                         return 1;
2829                 } else {
2830                         value_add( &c->rvalue_vals, c->be->be_suffix );
2831                         value_add( &c->rvalue_nvals, c->be->be_nsuffix );
2832                         return 0;
2833                 }
2834         } else if ( c->op == LDAP_MOD_DELETE ) {
2835                 if ( c->valx < 0 ) {
2836                         ber_bvarray_free( c->be->be_suffix );
2837                         ber_bvarray_free( c->be->be_nsuffix );
2838                         c->be->be_suffix = NULL;
2839                         c->be->be_nsuffix = NULL;
2840                 } else {
2841                         int i = c->valx;
2842                         ch_free( c->be->be_suffix[i].bv_val );
2843                         ch_free( c->be->be_nsuffix[i].bv_val );
2844                         do {
2845                                 c->be->be_suffix[i] = c->be->be_suffix[i+1];
2846                                 c->be->be_nsuffix[i] = c->be->be_nsuffix[i+1];
2847                                 i++;
2848                         } while ( !BER_BVISNULL( &c->be->be_suffix[i] ) );
2849                 }
2850                 return 0;
2851         }
2852
2853 #ifdef SLAPD_MONITOR_DN
2854         if(!strcasecmp(c->argv[1], SLAPD_MONITOR_DN)) {
2855                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> DN is reserved for monitoring slapd",
2856                         c->argv[0] );
2857                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
2858                         c->log, c->cr_msg, SLAPD_MONITOR_DN);
2859                 return(1);
2860         }
2861 #endif
2862
2863         if (SLAP_DB_ONE_SUFFIX( c->be ) && c->be->be_suffix ) {
2864                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> Only one suffix is allowed on this %s backend",
2865                         c->argv[0], c->be->bd_info->bi_type );
2866                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
2867                         c->log, c->cr_msg, 0);
2868                 return(1);
2869         }
2870
2871         pdn = c->value_dn;
2872         ndn = c->value_ndn;
2873
2874         if (SLAP_DBHIDDEN( c->be ))
2875                 tbe = NULL;
2876         else
2877                 tbe = select_backend(&ndn, 0);
2878         if(tbe == c->be) {
2879                 Debug( LDAP_DEBUG_ANY, "%s: suffix already served by this backend!.\n",
2880                         c->log, 0, 0);
2881                 return 1;
2882                 free(pdn.bv_val);
2883                 free(ndn.bv_val);
2884         } else if(tbe) {
2885                 BackendDB *b2 = tbe;
2886
2887                 /* Does tbe precede be? */
2888                 while (( b2 = LDAP_STAILQ_NEXT(b2, be_next )) && b2 && b2 != c->be );
2889
2890                 if ( b2 ) {
2891                         char    *type = tbe->bd_info->bi_type;
2892
2893                         if ( overlay_is_over( tbe ) ) {
2894                                 slap_overinfo   *oi = (slap_overinfo *)tbe->bd_info->bi_private;
2895                                 type = oi->oi_orig->bi_type;
2896                         }
2897
2898                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> namingContext \"%s\" "
2899                                 "already served by a preceding %s database",
2900                                 c->argv[0], pdn.bv_val, type );
2901                         Debug(LDAP_DEBUG_ANY, "%s: %s serving namingContext \"%s\"\n",
2902                                 c->log, c->cr_msg, tbe->be_suffix[0].bv_val);
2903                         free(pdn.bv_val);
2904                         free(ndn.bv_val);
2905                         return(1);
2906                 }
2907         }
2908         if(pdn.bv_len == 0 && default_search_nbase.bv_len) {
2909                 Debug(LDAP_DEBUG_ANY, "%s: suffix DN empty and default search "
2910                         "base provided \"%s\" (assuming okay)\n",
2911                         c->log, default_search_base.bv_val, 0);
2912         }
2913         ber_bvarray_add(&c->be->be_suffix, &pdn);
2914         ber_bvarray_add(&c->be->be_nsuffix, &ndn);
2915         return(0);
2916 }
2917
2918 static int
2919 config_rootdn(ConfigArgs *c) {
2920         if (c->op == SLAP_CONFIG_EMIT) {
2921                 if ( !BER_BVISNULL( &c->be->be_rootdn )) {
2922                         value_add_one(&c->rvalue_vals, &c->be->be_rootdn);
2923                         value_add_one(&c->rvalue_nvals, &c->be->be_rootndn);
2924                         return 0;
2925                 } else {
2926                         return 1;
2927                 }
2928         } else if ( c->op == LDAP_MOD_DELETE ) {
2929                 ch_free( c->be->be_rootdn.bv_val );
2930                 ch_free( c->be->be_rootndn.bv_val );
2931                 BER_BVZERO( &c->be->be_rootdn );
2932                 BER_BVZERO( &c->be->be_rootndn );
2933                 return 0;
2934         }
2935         if ( !BER_BVISNULL( &c->be->be_rootdn )) {
2936                 ch_free( c->be->be_rootdn.bv_val );
2937                 ch_free( c->be->be_rootndn.bv_val );
2938         }
2939         c->be->be_rootdn = c->value_dn;
2940         c->be->be_rootndn = c->value_ndn;
2941         return(0);
2942 }
2943
2944 static int
2945 config_rootpw(ConfigArgs *c) {
2946         Backend *tbe;
2947
2948         if (c->op == SLAP_CONFIG_EMIT) {
2949                 if (!BER_BVISEMPTY(&c->be->be_rootpw)) {
2950                         /* don't copy, because "rootpw" is marked
2951                          * as CFG_BERVAL */
2952                         c->value_bv = c->be->be_rootpw;
2953                         return 0;
2954                 }
2955                 return 1;
2956         } else if ( c->op == LDAP_MOD_DELETE ) {
2957                 ch_free( c->be->be_rootpw.bv_val );
2958                 BER_BVZERO( &c->be->be_rootpw );
2959                 return 0;
2960         }
2961
2962         tbe = select_backend(&c->be->be_rootndn, 0);
2963         if(tbe != c->be) {
2964                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> can only be set when rootdn is under suffix",
2965                         c->argv[0] );
2966                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
2967                         c->log, c->cr_msg, 0);
2968                 return(1);
2969         }
2970         if ( !BER_BVISNULL( &c->be->be_rootpw ))
2971                 ch_free( c->be->be_rootpw.bv_val );
2972         c->be->be_rootpw = c->value_bv;
2973         return(0);
2974 }
2975
2976 static int
2977 config_restrict(ConfigArgs *c) {
2978         slap_mask_t restrictops = 0;
2979         int i;
2980         slap_verbmasks restrictable_ops[] = {
2981                 { BER_BVC("bind"),              SLAP_RESTRICT_OP_BIND },
2982                 { BER_BVC("add"),               SLAP_RESTRICT_OP_ADD },
2983                 { BER_BVC("modify"),            SLAP_RESTRICT_OP_MODIFY },
2984                 { BER_BVC("rename"),            SLAP_RESTRICT_OP_RENAME },
2985                 { BER_BVC("modrdn"),            0 },
2986                 { BER_BVC("delete"),            SLAP_RESTRICT_OP_DELETE },
2987                 { BER_BVC("search"),            SLAP_RESTRICT_OP_SEARCH },
2988                 { BER_BVC("compare"),           SLAP_RESTRICT_OP_COMPARE },
2989                 { BER_BVC("read"),              SLAP_RESTRICT_OP_READS },
2990                 { BER_BVC("write"),             SLAP_RESTRICT_OP_WRITES },
2991                 { BER_BVC("extended"),          SLAP_RESTRICT_OP_EXTENDED },
2992                 { BER_BVC("extended=" LDAP_EXOP_START_TLS ),            SLAP_RESTRICT_EXOP_START_TLS },
2993                 { BER_BVC("extended=" LDAP_EXOP_MODIFY_PASSWD ),        SLAP_RESTRICT_EXOP_MODIFY_PASSWD },
2994                 { BER_BVC("extended=" LDAP_EXOP_X_WHO_AM_I ),           SLAP_RESTRICT_EXOP_WHOAMI },
2995                 { BER_BVC("extended=" LDAP_EXOP_X_CANCEL ),             SLAP_RESTRICT_EXOP_CANCEL },
2996                 { BER_BVC("all"),               SLAP_RESTRICT_OP_ALL },
2997                 { BER_BVNULL,   0 }
2998         };
2999
3000         if (c->op == SLAP_CONFIG_EMIT) {
3001                 return mask_to_verbs( restrictable_ops, c->be->be_restrictops,
3002                         &c->rvalue_vals );
3003         } else if ( c->op == LDAP_MOD_DELETE ) {
3004                 if ( !c->line ) {
3005                         c->be->be_restrictops = 0;
3006                 } else {
3007                         restrictops = verb_to_mask( c->line, restrictable_ops );
3008                         c->be->be_restrictops ^= restrictops;
3009                 }
3010                 return 0;
3011         }
3012         i = verbs_to_mask( c->argc, c->argv, restrictable_ops, &restrictops );
3013         if ( i ) {
3014                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown operation", c->argv[0] );
3015                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
3016                         c->log, c->cr_msg, c->argv[i]);
3017                 return(1);
3018         }
3019         if ( restrictops & SLAP_RESTRICT_OP_EXTENDED )
3020                 restrictops &= ~SLAP_RESTRICT_EXOP_MASK;
3021         c->be->be_restrictops |= restrictops;
3022         return(0);
3023 }
3024
3025 static int
3026 config_allows(ConfigArgs *c) {
3027         slap_mask_t allows = 0;
3028         int i;
3029         slap_verbmasks allowable_ops[] = {
3030                 { BER_BVC("bind_v2"),           SLAP_ALLOW_BIND_V2 },
3031                 { BER_BVC("bind_anon_cred"),    SLAP_ALLOW_BIND_ANON_CRED },
3032                 { BER_BVC("bind_anon_dn"),      SLAP_ALLOW_BIND_ANON_DN },
3033                 { BER_BVC("update_anon"),       SLAP_ALLOW_UPDATE_ANON },
3034                 { BER_BVC("proxy_authz_anon"),  SLAP_ALLOW_PROXY_AUTHZ_ANON },
3035                 { BER_BVNULL,   0 }
3036         };
3037         if (c->op == SLAP_CONFIG_EMIT) {
3038                 return mask_to_verbs( allowable_ops, global_allows, &c->rvalue_vals );
3039         } else if ( c->op == LDAP_MOD_DELETE ) {
3040                 if ( !c->line ) {
3041                         global_allows = 0;
3042                 } else {
3043                         allows = verb_to_mask( c->line, allowable_ops );
3044                         global_allows ^= allows;
3045                 }
3046                 return 0;
3047         }
3048         i = verbs_to_mask(c->argc, c->argv, allowable_ops, &allows);
3049         if ( i ) {
3050                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown feature", c->argv[0] );
3051                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
3052                         c->log, c->cr_msg, c->argv[i]);
3053                 return(1);
3054         }
3055         global_allows |= allows;
3056         return(0);
3057 }
3058
3059 static int
3060 config_disallows(ConfigArgs *c) {
3061         slap_mask_t disallows = 0;
3062         int i;
3063         slap_verbmasks disallowable_ops[] = {
3064                 { BER_BVC("bind_anon"),         SLAP_DISALLOW_BIND_ANON },
3065                 { BER_BVC("bind_simple"),       SLAP_DISALLOW_BIND_SIMPLE },
3066                 { BER_BVC("tls_2_anon"),                SLAP_DISALLOW_TLS_2_ANON },
3067                 { BER_BVC("tls_authc"),         SLAP_DISALLOW_TLS_AUTHC },
3068                 { BER_BVC("proxy_authz_non_critical"),  SLAP_DISALLOW_PROXY_AUTHZ_N_CRIT },
3069                 { BER_BVC("dontusecopy_non_critical"),  SLAP_DISALLOW_DONTUSECOPY_N_CRIT },
3070                 { BER_BVNULL, 0 }
3071         };
3072         if (c->op == SLAP_CONFIG_EMIT) {
3073                 return mask_to_verbs( disallowable_ops, global_disallows, &c->rvalue_vals );
3074         } else if ( c->op == LDAP_MOD_DELETE ) {
3075                 if ( !c->line ) {
3076                         global_disallows = 0;
3077                 } else {
3078                         disallows = verb_to_mask( c->line, disallowable_ops );
3079                         global_disallows ^= disallows;
3080                 }
3081                 return 0;
3082         }
3083         i = verbs_to_mask(c->argc, c->argv, disallowable_ops, &disallows);
3084         if ( i ) {
3085                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown feature", c->argv[0] );
3086                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
3087                         c->log, c->cr_msg, c->argv[i]);
3088                 return(1);
3089         }
3090         global_disallows |= disallows;
3091         return(0);
3092 }
3093
3094 static int
3095 config_requires(ConfigArgs *c) {
3096         slap_mask_t requires = frontendDB->be_requires;
3097         int i, argc = c->argc;
3098         char **argv = c->argv;
3099
3100         slap_verbmasks requires_ops[] = {
3101                 { BER_BVC("bind"),              SLAP_REQUIRE_BIND },
3102                 { BER_BVC("LDAPv3"),            SLAP_REQUIRE_LDAP_V3 },
3103                 { BER_BVC("authc"),             SLAP_REQUIRE_AUTHC },
3104                 { BER_BVC("sasl"),              SLAP_REQUIRE_SASL },
3105                 { BER_BVC("strong"),            SLAP_REQUIRE_STRONG },
3106                 { BER_BVNULL, 0 }
3107         };
3108         if (c->op == SLAP_CONFIG_EMIT) {
3109                 return mask_to_verbs( requires_ops, c->be->be_requires, &c->rvalue_vals );
3110         } else if ( c->op == LDAP_MOD_DELETE ) {
3111                 if ( !c->line ) {
3112                         c->be->be_requires = 0;
3113                 } else {
3114                         requires = verb_to_mask( c->line, requires_ops );
3115                         c->be->be_requires ^= requires;
3116                 }
3117                 return 0;
3118         }
3119         /* "none" can only be first, to wipe out default/global values */
3120         if ( strcasecmp( c->argv[ 1 ], "none" ) == 0 ) {
3121                 argv++;
3122                 argc--;
3123                 requires = 0;
3124         }
3125         i = verbs_to_mask(argc, argv, requires_ops, &requires);
3126         if ( i ) {
3127                 if (strcasecmp( c->argv[ i ], "none" ) == 0 ) {
3128                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> \"none\" (#%d) must be listed first", c->argv[0], i - 1 );
3129                         Debug(LDAP_DEBUG_ANY, "%s: %s\n",
3130                                 c->log, c->cr_msg, 0);
3131                 } else {
3132                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown feature #%d", c->argv[0], i - 1 );
3133                         Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
3134                                 c->log, c->cr_msg, c->argv[i]);
3135                 }
3136                 return(1);
3137         }
3138         c->be->be_requires = requires;
3139         return(0);
3140 }
3141
3142 static slap_verbmasks   *loglevel_ops;
3143
3144 static int
3145 loglevel_init( void )
3146 {
3147         slap_verbmasks  lo[] = {
3148                 { BER_BVC("Any"),       (slap_mask_t) LDAP_DEBUG_ANY },
3149                 { BER_BVC("Trace"),     LDAP_DEBUG_TRACE },
3150                 { BER_BVC("Packets"),   LDAP_DEBUG_PACKETS },
3151                 { BER_BVC("Args"),      LDAP_DEBUG_ARGS },
3152                 { BER_BVC("Conns"),     LDAP_DEBUG_CONNS },
3153                 { BER_BVC("BER"),       LDAP_DEBUG_BER },
3154                 { BER_BVC("Filter"),    LDAP_DEBUG_FILTER },
3155                 { BER_BVC("Config"),    LDAP_DEBUG_CONFIG },
3156                 { BER_BVC("ACL"),       LDAP_DEBUG_ACL },
3157                 { BER_BVC("Stats"),     LDAP_DEBUG_STATS },
3158                 { BER_BVC("Stats2"),    LDAP_DEBUG_STATS2 },
3159                 { BER_BVC("Shell"),     LDAP_DEBUG_SHELL },
3160                 { BER_BVC("Parse"),     LDAP_DEBUG_PARSE },
3161 #if 0   /* no longer used (nor supported) */
3162                 { BER_BVC("Cache"),     LDAP_DEBUG_CACHE },
3163                 { BER_BVC("Index"),     LDAP_DEBUG_INDEX },
3164 #endif
3165                 { BER_BVC("Sync"),      LDAP_DEBUG_SYNC },
3166                 { BER_BVC("None"),      LDAP_DEBUG_NONE },
3167                 { BER_BVNULL,           0 }
3168         };
3169
3170         return slap_verbmasks_init( &loglevel_ops, lo );
3171 }
3172
3173 static void
3174 loglevel_destroy( void )
3175 {
3176         if ( loglevel_ops ) {
3177                 (void)slap_verbmasks_destroy( loglevel_ops );
3178         }
3179         loglevel_ops = NULL;
3180 }
3181
3182 static slap_mask_t      loglevel_ignore[] = { -1, 0 };
3183
3184 int
3185 slap_loglevel_register( slap_mask_t m, struct berval *s )
3186 {
3187         int     rc;
3188
3189         if ( loglevel_ops == NULL ) {
3190                 loglevel_init();
3191         }
3192
3193         rc = slap_verbmasks_append( &loglevel_ops, m, s, loglevel_ignore );
3194
3195         if ( rc != 0 ) {
3196                 Debug( LDAP_DEBUG_ANY, "slap_loglevel_register(%lu, \"%s\") failed\n",
3197                         m, s->bv_val, 0 );
3198         }
3199
3200         return rc;
3201 }
3202
3203 int
3204 slap_loglevel_get( struct berval *s, int *l )
3205 {
3206         int             rc;
3207         slap_mask_t     m, i;
3208
3209         if ( loglevel_ops == NULL ) {
3210                 loglevel_init();
3211         }
3212
3213         for ( m = 0, i = 1; !BER_BVISNULL( &loglevel_ops[ i ].word ); i++ ) {
3214                 m |= loglevel_ops[ i ].mask;
3215         }
3216
3217         for ( i = 1; m & i; i <<= 1 )
3218                 ;
3219
3220         if ( i == 0 ) {
3221                 return -1;
3222         }
3223
3224         rc = slap_verbmasks_append( &loglevel_ops, i, s, loglevel_ignore );
3225
3226         if ( rc != 0 ) {
3227                 Debug( LDAP_DEBUG_ANY, "slap_loglevel_get(%lu, \"%s\") failed\n",
3228                         i, s->bv_val, 0 );
3229
3230         } else {
3231                 *l = i;
3232         }
3233
3234         return rc;
3235 }
3236
3237 int
3238 str2loglevel( const char *s, int *l )
3239 {
3240         int     i;
3241
3242         if ( loglevel_ops == NULL ) {
3243                 loglevel_init();
3244         }
3245
3246         i = verb_to_mask( s, loglevel_ops );
3247
3248         if ( BER_BVISNULL( &loglevel_ops[ i ].word ) ) {
3249                 return -1;
3250         }
3251
3252         *l = loglevel_ops[ i ].mask;
3253
3254         return 0;
3255 }
3256
3257 const char *
3258 loglevel2str( int l )
3259 {
3260         struct berval   bv = BER_BVNULL;
3261
3262         loglevel2bv( l, &bv );
3263
3264         return bv.bv_val;
3265 }
3266
3267 int
3268 loglevel2bv( int l, struct berval *bv )
3269 {
3270         if ( loglevel_ops == NULL ) {
3271                 loglevel_init();
3272         }
3273
3274         BER_BVZERO( bv );
3275
3276         return enum_to_verb( loglevel_ops, l, bv ) == -1;
3277 }
3278
3279 int
3280 loglevel2bvarray( int l, BerVarray *bva )
3281 {
3282         if ( loglevel_ops == NULL ) {
3283                 loglevel_init();
3284         }
3285
3286         return mask_to_verbs( loglevel_ops, l, bva );
3287 }
3288
3289 int
3290 loglevel_print( FILE *out )
3291 {
3292         int     i;
3293
3294         if ( loglevel_ops == NULL ) {
3295                 loglevel_init();
3296         }
3297
3298         fprintf( out, "Installed log subsystems:\n\n" );
3299         for ( i = 0; !BER_BVISNULL( &loglevel_ops[ i ].word ); i++ ) {
3300                 unsigned mask = loglevel_ops[ i ].mask & 0xffffffffUL;
3301                 fprintf( out,
3302                         (mask == ((slap_mask_t) -1 & 0xffffffffUL)
3303                          ? "\t%-30s (-1, 0xffffffff)\n" : "\t%-30s (%u, 0x%x)\n"),
3304                         loglevel_ops[ i ].word.bv_val, mask, mask );
3305         }
3306
3307         fprintf( out, "\nNOTE: custom log subsystems may be later installed "
3308                 "by specific code\n\n" );
3309
3310         return 0;
3311 }
3312
3313 static int config_syslog;
3314
3315 static int
3316 config_loglevel(ConfigArgs *c) {
3317         int i;
3318
3319         if ( loglevel_ops == NULL ) {
3320                 loglevel_init();
3321         }
3322
3323         if (c->op == SLAP_CONFIG_EMIT) {
3324                 /* Get default or commandline slapd setting */
3325                 if ( ldap_syslog && !config_syslog )
3326                         config_syslog = ldap_syslog;
3327                 return loglevel2bvarray( config_syslog, &c->rvalue_vals );
3328
3329         } else if ( c->op == LDAP_MOD_DELETE ) {
3330                 if ( !c->line ) {
3331                         config_syslog = 0;
3332                 } else {
3333                         int level = verb_to_mask( c->line, loglevel_ops );
3334                         config_syslog ^= level;
3335                 }
3336                 if ( slapMode & SLAP_SERVER_MODE ) {
3337                         ldap_syslog = config_syslog;
3338                 }
3339                 return 0;
3340         }
3341
3342         for( i=1; i < c->argc; i++ ) {
3343                 int     level;
3344
3345                 if ( isdigit((unsigned char)c->argv[i][0]) || c->argv[i][0] == '-' ) {
3346                         if( lutil_atoix( &level, c->argv[i], 0 ) != 0 ) {
3347                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to parse level", c->argv[0] );
3348                                 Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
3349                                         c->log, c->cr_msg, c->argv[i]);
3350                                 return( 1 );
3351                         }
3352                 } else {
3353                         if ( str2loglevel( c->argv[i], &level ) ) {
3354                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown level", c->argv[0] );
3355                                 Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
3356                                         c->log, c->cr_msg, c->argv[i]);
3357                                 return( 1 );
3358                         }
3359                 }
3360                 /* Explicitly setting a zero clears all the levels */
3361                 if ( level )
3362                         config_syslog |= level;
3363                 else
3364                         config_syslog = 0;
3365         }
3366         if ( slapMode & SLAP_SERVER_MODE ) {
3367                 ldap_syslog = config_syslog;
3368         }
3369         return(0);
3370 }
3371
3372 static int
3373 config_referral(ConfigArgs *c) {
3374         struct berval val;
3375         if (c->op == SLAP_CONFIG_EMIT) {
3376                 if ( default_referral ) {
3377                         value_add( &c->rvalue_vals, default_referral );
3378                         return 0;
3379                 } else {
3380                         return 1;
3381                 }
3382         } else if ( c->op == LDAP_MOD_DELETE ) {
3383                 if ( c->valx < 0 ) {
3384                         ber_bvarray_free( default_referral );
3385                         default_referral = NULL;
3386                 } else {
3387                         int i = c->valx;
3388                         ch_free( default_referral[i].bv_val );
3389                         for (; default_referral[i].bv_val; i++ )
3390                                 default_referral[i] = default_referral[i+1];
3391                 }
3392                 return 0;
3393         }
3394         if(validate_global_referral(c->argv[1])) {
3395                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid URL", c->argv[0] );
3396                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
3397                         c->log, c->cr_msg, c->argv[1]);
3398                 return(1);
3399         }
3400
3401         ber_str2bv(c->argv[1], 0, 0, &val);
3402         if(value_add_one(&default_referral, &val)) return(LDAP_OTHER);
3403         return(0);
3404 }
3405
3406 static struct {
3407         struct berval key;
3408         int off;
3409 } sec_keys[] = {
3410         { BER_BVC("ssf="), offsetof(slap_ssf_set_t, sss_ssf) },
3411         { BER_BVC("transport="), offsetof(slap_ssf_set_t, sss_transport) },
3412         { BER_BVC("tls="), offsetof(slap_ssf_set_t, sss_tls) },
3413         { BER_BVC("sasl="), offsetof(slap_ssf_set_t, sss_sasl) },
3414         { BER_BVC("update_ssf="), offsetof(slap_ssf_set_t, sss_update_ssf) },
3415         { BER_BVC("update_transport="), offsetof(slap_ssf_set_t, sss_update_transport) },
3416         { BER_BVC("update_tls="), offsetof(slap_ssf_set_t, sss_update_tls) },
3417         { BER_BVC("update_sasl="), offsetof(slap_ssf_set_t, sss_update_sasl) },
3418         { BER_BVC("simple_bind="), offsetof(slap_ssf_set_t, sss_simple_bind) },
3419         { BER_BVNULL, 0 }
3420 };
3421
3422 static int
3423 config_security(ConfigArgs *c) {
3424         slap_ssf_set_t *set = &c->be->be_ssf_set;
3425         char *next;
3426         int i, j;
3427         if (c->op == SLAP_CONFIG_EMIT) {
3428                 char numbuf[32];
3429                 struct berval bv;
3430                 slap_ssf_t *tgt;
3431                 int rc = 1;
3432
3433                 for (i=0; !BER_BVISNULL( &sec_keys[i].key ); i++) {
3434                         tgt = (slap_ssf_t *)((char *)set + sec_keys[i].off);
3435                         if ( *tgt ) {
3436                                 rc = 0;
3437                                 bv.bv_len = snprintf( numbuf, sizeof( numbuf ), "%u", *tgt );
3438                                 if ( bv.bv_len >= sizeof( numbuf ) ) {
3439                                         ber_bvarray_free_x( c->rvalue_vals, NULL );
3440                                         c->rvalue_vals = NULL;
3441                                         rc = 1;
3442                                         break;
3443                                 }
3444                                 bv.bv_len += sec_keys[i].key.bv_len;
3445                                 bv.bv_val = ch_malloc( bv.bv_len + 1);
3446                                 next = lutil_strcopy( bv.bv_val, sec_keys[i].key.bv_val );
3447                                 strcpy( next, numbuf );
3448                                 ber_bvarray_add( &c->rvalue_vals, &bv );
3449                         }
3450                 }
3451                 return rc;
3452         }
3453         for(i = 1; i < c->argc; i++) {
3454                 slap_ssf_t *tgt = NULL;
3455                 char *src = NULL;
3456                 for ( j=0; !BER_BVISNULL( &sec_keys[j].key ); j++ ) {
3457                         if(!strncasecmp(c->argv[i], sec_keys[j].key.bv_val,
3458                                 sec_keys[j].key.bv_len)) {
3459                                 src = c->argv[i] + sec_keys[j].key.bv_len;
3460                                 tgt = (slap_ssf_t *)((char *)set + sec_keys[j].off);
3461                                 break;
3462                         }
3463                 }
3464                 if ( !tgt ) {
3465                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unknown factor", c->argv[0] );
3466                         Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
3467                                 c->log, c->cr_msg, c->argv[i]);
3468                         return(1);
3469                 }
3470
3471                 if ( lutil_atou( tgt, src ) != 0 ) {
3472                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> unable to parse factor", c->argv[0] );
3473                         Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
3474                                 c->log, c->cr_msg, c->argv[i]);
3475                         return(1);
3476                 }
3477         }
3478         return(0);
3479 }
3480
3481 char *
3482 anlist_unparse( AttributeName *an, char *ptr, ber_len_t buflen ) {
3483         int comma = 0;
3484         char *start = ptr;
3485
3486         for (; !BER_BVISNULL( &an->an_name ); an++) {
3487                 /* if buflen == 0, assume the buffer size has been 
3488                  * already checked otherwise */
3489                 if ( buflen > 0 && buflen - ( ptr - start ) < comma + an->an_name.bv_len ) return NULL;
3490                 if ( comma ) *ptr++ = ',';
3491                 ptr = lutil_strcopy( ptr, an->an_name.bv_val );
3492                 comma = 1;
3493         }
3494         return ptr;
3495 }
3496
3497 static int
3498 config_updatedn(ConfigArgs *c) {
3499         if (c->op == SLAP_CONFIG_EMIT) {
3500                 if (!BER_BVISEMPTY(&c->be->be_update_ndn)) {
3501                         value_add_one(&c->rvalue_vals, &c->be->be_update_ndn);
3502                         value_add_one(&c->rvalue_nvals, &c->be->be_update_ndn);
3503                         return 0;
3504                 }
3505                 return 1;
3506         } else if ( c->op == LDAP_MOD_DELETE ) {
3507                 ch_free( c->be->be_update_ndn.bv_val );
3508                 BER_BVZERO( &c->be->be_update_ndn );
3509                 SLAP_DBFLAGS(c->be) ^= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SLURP_SHADOW);
3510                 return 0;
3511         }
3512         if(SLAP_SHADOW(c->be)) {
3513                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> database already shadowed", c->argv[0] );
3514                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
3515                         c->log, c->cr_msg, 0);
3516                 return(1);
3517         }
3518
3519         ber_memfree_x( c->value_dn.bv_val, NULL );
3520         if ( !BER_BVISNULL( &c->be->be_update_ndn ) ) {
3521                 ber_memfree_x( c->be->be_update_ndn.bv_val, NULL );
3522         }
3523         c->be->be_update_ndn = c->value_ndn;
3524         BER_BVZERO( &c->value_dn );
3525         BER_BVZERO( &c->value_ndn );
3526
3527         return config_slurp_shadow( c );
3528 }
3529
3530 int
3531 config_shadow( ConfigArgs *c, slap_mask_t flag )
3532 {
3533         char    *notallowed = NULL;
3534
3535         if ( c->be == frontendDB ) {
3536                 notallowed = "frontend";
3537
3538         } else if ( SLAP_MONITOR(c->be) ) {
3539                 notallowed = "monitor";
3540         }
3541
3542         if ( notallowed != NULL ) {
3543                 Debug( LDAP_DEBUG_ANY, "%s: %s database cannot be shadow.\n", c->log, notallowed, 0 );
3544                 return 1;
3545         }
3546
3547         if ( SLAP_SHADOW(c->be) ) {
3548                 /* if already shadow, only check consistency */
3549                 if ( ( SLAP_DBFLAGS(c->be) & flag ) != flag ) {
3550                         Debug( LDAP_DEBUG_ANY, "%s: inconsistent shadow flag 0x%lx.\n",
3551                                 c->log, flag, 0 );
3552                         return 1;
3553                 }
3554
3555         } else {
3556                 SLAP_DBFLAGS(c->be) |= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SINGLE_SHADOW | flag);
3557         }
3558
3559         return 0;
3560 }
3561
3562 static int
3563 config_updateref(ConfigArgs *c) {
3564         struct berval val;
3565         if (c->op == SLAP_CONFIG_EMIT) {
3566                 if ( c->be->be_update_refs ) {
3567                         value_add( &c->rvalue_vals, c->be->be_update_refs );
3568                         return 0;
3569                 } else {
3570                         return 1;
3571                 }
3572         } else if ( c->op == LDAP_MOD_DELETE ) {
3573                 if ( c->valx < 0 ) {
3574                         ber_bvarray_free( c->be->be_update_refs );
3575                         c->be->be_update_refs = NULL;
3576                 } else {
3577                         int i = c->valx;
3578                         ch_free( c->be->be_update_refs[i].bv_val );
3579                         for (; c->be->be_update_refs[i].bv_val; i++)
3580                                 c->be->be_update_refs[i] = c->be->be_update_refs[i+1];
3581                 }
3582                 return 0;
3583         }
3584         if(!SLAP_SHADOW(c->be) && !c->be->be_syncinfo) {
3585                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> must appear after syncrepl or updatedn",
3586                         c->argv[0] );
3587                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
3588                         c->log, c->cr_msg, 0);
3589                 return(1);
3590         }
3591
3592         if(validate_global_referral(c->argv[1])) {
3593                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> invalid URL", c->argv[0] );
3594                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
3595                         c->log, c->cr_msg, c->argv[1]);
3596                 return(1);
3597         }
3598         ber_str2bv(c->argv[1], 0, 0, &val);
3599         if(value_add_one(&c->be->be_update_refs, &val)) return(LDAP_OTHER);
3600         return(0);
3601 }
3602
3603 static int
3604 config_obsolete(ConfigArgs *c) {
3605         if (c->op == SLAP_CONFIG_EMIT)
3606                 return 1;
3607
3608         snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> keyword is obsolete (ignored)",
3609                 c->argv[0] );
3610         Debug(LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0);
3611         return(0);
3612 }
3613
3614 static int
3615 config_include(ConfigArgs *c) {
3616         int savelineno = c->lineno;
3617         int rc;
3618         ConfigFile *cf;
3619         ConfigFile *cfsave = cfn;
3620         ConfigFile *cf2 = NULL;
3621
3622         /* Leftover from RE23. No dynamic config for include files */
3623         if ( c->op == SLAP_CONFIG_EMIT || c->op == LDAP_MOD_DELETE )
3624                 return 1;
3625
3626         cf = ch_calloc( 1, sizeof(ConfigFile));
3627         if ( cfn->c_kids ) {
3628                 for (cf2=cfn->c_kids; cf2 && cf2->c_sibs; cf2=cf2->c_sibs) ;
3629                 cf2->c_sibs = cf;
3630         } else {
3631                 cfn->c_kids = cf;
3632         }
3633         cfn = cf;
3634         ber_str2bv( c->argv[1], 0, 1, &cf->c_file );
3635         rc = read_config_file(c->argv[1], c->depth + 1, c, config_back_cf_table);
3636         c->lineno = savelineno - 1;
3637         cfn = cfsave;
3638         if ( rc ) {
3639                 if ( cf2 ) cf2->c_sibs = NULL;
3640                 else cfn->c_kids = NULL;
3641                 ch_free( cf->c_file.bv_val );
3642                 ch_free( cf );
3643         } else {
3644                 c->ca_private = cf;
3645         }
3646         return(rc);
3647 }
3648
3649 #ifdef HAVE_TLS
3650 static int
3651 config_tls_cleanup(ConfigArgs *c) {
3652         int rc = 0;
3653
3654         if ( slap_tls_ld ) {
3655                 int opt = 1;
3656
3657                 ldap_pvt_tls_ctx_free( slap_tls_ctx );
3658
3659                 /* Force new ctx to be created */
3660                 rc = ldap_pvt_tls_set_option( slap_tls_ld, LDAP_OPT_X_TLS_NEWCTX, &opt );
3661                 if( rc == 0 ) {
3662                         /* The ctx's refcount is bumped up here */
3663                         ldap_pvt_tls_get_option( slap_tls_ld, LDAP_OPT_X_TLS_CTX, &slap_tls_ctx );
3664                         /* This is a no-op if it's already loaded */
3665                         load_extop( &slap_EXOP_START_TLS, 0, starttls_extop );
3666                 }
3667         }
3668         return rc;
3669 }
3670
3671 static int
3672 config_tls_option(ConfigArgs *c) {
3673         int flag;
3674         LDAP *ld = slap_tls_ld;
3675         switch(c->type) {
3676         case CFG_TLS_RAND:      flag = LDAP_OPT_X_TLS_RANDOM_FILE;      ld = NULL; break;
3677         case CFG_TLS_CIPHER:    flag = LDAP_OPT_X_TLS_CIPHER_SUITE;     break;
3678         case CFG_TLS_CERT_FILE: flag = LDAP_OPT_X_TLS_CERTFILE;         break;  
3679         case CFG_TLS_CERT_KEY:  flag = LDAP_OPT_X_TLS_KEYFILE;          break;
3680         case CFG_TLS_CA_PATH:   flag = LDAP_OPT_X_TLS_CACERTDIR;        break;
3681         case CFG_TLS_CA_FILE:   flag = LDAP_OPT_X_TLS_CACERTFILE;       break;
3682         case CFG_TLS_DH_FILE:   flag = LDAP_OPT_X_TLS_DHFILE;   break;
3683 #ifdef HAVE_GNUTLS
3684         case CFG_TLS_CRL_FILE:  flag = LDAP_OPT_X_TLS_CRLFILE;  break;
3685 #endif
3686         default:                Debug(LDAP_DEBUG_ANY, "%s: "
3687                                         "unknown tls_option <0x%x>\n",
3688                                         c->log, c->type, 0);
3689                 return 1;
3690         }
3691         if (c->op == SLAP_CONFIG_EMIT) {
3692                 return ldap_pvt_tls_get_option( ld, flag, &c->value_string );
3693         } else if ( c->op == LDAP_MOD_DELETE ) {
3694                 c->cleanup = config_tls_cleanup;
3695                 return ldap_pvt_tls_set_option( ld, flag, NULL );
3696         }
3697         ch_free(c->value_string);
3698         c->cleanup = config_tls_cleanup;
3699         return(ldap_pvt_tls_set_option(ld, flag, c->argv[1]));
3700 }
3701
3702 /* FIXME: this ought to be provided by libldap */
3703 static int
3704 config_tls_config(ConfigArgs *c) {
3705         int i, flag;
3706         switch(c->type) {
3707         case CFG_TLS_CRLCHECK:  flag = LDAP_OPT_X_TLS_CRLCHECK; break;
3708         case CFG_TLS_VERIFY:    flag = LDAP_OPT_X_TLS_REQUIRE_CERT; break;
3709         case CFG_TLS_PROTOCOL_MIN: flag = LDAP_OPT_X_TLS_PROTOCOL_MIN; break;
3710         default:
3711                 Debug(LDAP_DEBUG_ANY, "%s: "
3712                                 "unknown tls_option <0x%x>\n",
3713                                 c->log, c->type, 0);
3714                 return 1;
3715         }
3716         if (c->op == SLAP_CONFIG_EMIT) {
3717                 return slap_tls_get_config( slap_tls_ld, flag, &c->value_string );
3718         } else if ( c->op == LDAP_MOD_DELETE ) {
3719                 int i = 0;
3720                 c->cleanup = config_tls_cleanup;
3721                 return ldap_pvt_tls_set_option( slap_tls_ld, flag, &i );
3722         }
3723         ch_free( c->value_string );
3724         c->cleanup = config_tls_cleanup;
3725         if ( isdigit( (unsigned char)c->argv[1][0] ) ) {
3726                 if ( lutil_atoi( &i, c->argv[1] ) != 0 ) {
3727                         Debug(LDAP_DEBUG_ANY, "%s: "
3728                                 "unable to parse %s \"%s\"\n",
3729                                 c->log, c->argv[0], c->argv[1] );
3730                         return 1;
3731                 }
3732                 return(ldap_pvt_tls_set_option(slap_tls_ld, flag, &i));
3733         } else {
3734                 return(ldap_pvt_tls_config(slap_tls_ld, flag, c->argv[1]));
3735         }
3736 }
3737 #endif
3738
3739 static CfEntryInfo *
3740 config_find_base( CfEntryInfo *root, struct berval *dn, CfEntryInfo **last )
3741 {
3742         struct berval cdn;
3743         char *c;
3744
3745         if ( !root ) {
3746                 *last = NULL;
3747                 return NULL;
3748         }
3749
3750         if ( dn_match( &root->ce_entry->e_nname, dn ))
3751                 return root;
3752
3753         c = dn->bv_val+dn->bv_len;
3754         for (;*c != ',';c--);
3755
3756         while(root) {
3757                 *last = root;
3758                 for (--c;c>dn->bv_val && *c != ',';c--);
3759                 cdn.bv_val = c;
3760                 if ( *c == ',' )
3761                         cdn.bv_val++;
3762                 cdn.bv_len = dn->bv_len - (cdn.bv_val - dn->bv_val);
3763
3764                 root = root->ce_kids;
3765
3766                 for (;root;root=root->ce_sibs) {
3767                         if ( dn_match( &root->ce_entry->e_nname, &cdn )) {
3768                                 if ( cdn.bv_val == dn->bv_val ) {
3769                                         return root;
3770                                 }
3771                                 break;
3772                         }
3773                 }
3774         }
3775         return root;
3776 }
3777
3778 typedef struct setup_cookie {
3779         CfBackInfo *cfb;
3780         ConfigArgs *ca;
3781         Entry *frontend;
3782         Entry *config;
3783         int got_frontend;
3784         int got_config;
3785 } setup_cookie;
3786
3787 static int
3788 config_ldif_resp( Operation *op, SlapReply *rs )
3789 {
3790         if ( rs->sr_type == REP_SEARCH ) {
3791                 setup_cookie *sc = op->o_callback->sc_private;
3792                 struct berval pdn;
3793
3794                 sc->cfb->cb_got_ldif = 1;
3795                 /* Does the frontend exist? */
3796                 if ( !sc->got_frontend ) {
3797                         if ( !strncmp( rs->sr_entry->e_nname.bv_val,
3798                                 "olcDatabase", STRLENOF( "olcDatabase" )))
3799                         {
3800                                 if ( strncmp( rs->sr_entry->e_nname.bv_val +
3801                                         STRLENOF( "olcDatabase" ), "={-1}frontend",
3802                                         STRLENOF( "={-1}frontend" )))
3803                                 {
3804                                         struct berval rdn;
3805                                         int i = op->o_noop;
3806                                         sc->ca->be = frontendDB;
3807                                         sc->ca->bi = frontendDB->bd_info;
3808                                         frontendDB->be_cf_ocs = &CFOC_FRONTEND;
3809                                         rdn.bv_val = sc->ca->log;
3810                                         rdn.bv_len = snprintf(rdn.bv_val, sizeof( sc->ca->log ),
3811                                                 "%s=" SLAP_X_ORDERED_FMT "%s",
3812                                                 cfAd_database->ad_cname.bv_val, -1,
3813                                                 sc->ca->bi->bi_type);
3814                                         op->o_noop = 1;
3815                                         sc->frontend = config_build_entry( op, rs,
3816                                                 sc->cfb->cb_root, sc->ca, &rdn, &CFOC_DATABASE,
3817                                                 sc->ca->be->be_cf_ocs );
3818                                         op->o_noop = i;
3819                                         sc->got_frontend++;
3820                                 } else {
3821                                         sc->got_frontend++;
3822                                         goto ok;
3823                                 }
3824                         }
3825                 }
3826
3827                 dnParent( &rs->sr_entry->e_nname, &pdn );
3828
3829                 /* Does the configDB exist? */
3830                 if ( sc->got_frontend && !sc->got_config &&
3831                         !strncmp( rs->sr_entry->e_nname.bv_val,
3832                         "olcDatabase", STRLENOF( "olcDatabase" )) &&
3833                         dn_match( &config_rdn, &pdn ) )
3834                 {
3835                         if ( strncmp( rs->sr_entry->e_nname.bv_val +
3836                                 STRLENOF( "olcDatabase" ), "={0}config",
3837                                 STRLENOF( "={0}config" )))
3838                         {
3839                                 struct berval rdn;
3840                                 int i = op->o_noop;
3841                                 sc->ca->be = LDAP_STAILQ_FIRST( &backendDB );
3842                                 sc->ca->bi = sc->ca->be->bd_info;
3843                                 rdn.bv_val = sc->ca->log;
3844                                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( sc->ca->log ),
3845                                         "%s=" SLAP_X_ORDERED_FMT "%s",
3846                                         cfAd_database->ad_cname.bv_val, 0,
3847                                         sc->ca->bi->bi_type);
3848                                 op->o_noop = 1;
3849                                 sc->config = config_build_entry( op, rs, sc->cfb->cb_root,
3850                                         sc->ca, &rdn, &CFOC_DATABASE, sc->ca->be->be_cf_ocs );
3851                                 op->o_noop = i;
3852                         }
3853                         sc->got_config++;
3854                 }
3855
3856 ok:
3857                 rs->sr_err = config_add_internal( sc->cfb, rs->sr_entry, sc->ca, NULL, NULL, NULL );
3858                 if ( rs->sr_err != LDAP_SUCCESS ) {
3859                         Debug( LDAP_DEBUG_ANY, "config error processing %s: %s\n",
3860                                 rs->sr_entry->e_name.bv_val, sc->ca->cr_msg, 0 );
3861                 }
3862         }
3863         return rs->sr_err;
3864 }
3865
3866 /* Configure and read the underlying back-ldif store */
3867 static int
3868 config_setup_ldif( BackendDB *be, const char *dir, int readit ) {
3869         CfBackInfo *cfb = be->be_private;
3870         ConfigArgs c = {0};
3871         ConfigTable *ct;
3872         char *argv[3];
3873         int rc = 0;
3874         setup_cookie sc;
3875         slap_callback cb = { NULL, config_ldif_resp, NULL, NULL };
3876         Connection conn = {0};
3877         OperationBuffer opbuf;
3878         Operation *op;
3879         SlapReply rs = {REP_RESULT};
3880         Filter filter = { LDAP_FILTER_PRESENT };
3881         struct berval filterstr = BER_BVC("(objectclass=*)");
3882         struct stat st;
3883
3884         /* Is the config directory available? */
3885         if ( stat( dir, &st ) < 0 ) {
3886                 /* No, so don't bother using the backing store.
3887                  * All changes will be in-memory only.
3888                  */
3889                 return 0;
3890         }
3891                 
3892         cfb->cb_db.bd_info = backend_info( "ldif" );
3893         if ( !cfb->cb_db.bd_info )
3894                 return 0;       /* FIXME: eventually this will be a fatal error */
3895
3896         if ( backend_db_init( "ldif", &cfb->cb_db, -1, NULL ) == NULL )
3897                 return 1;
3898
3899         cfb->cb_db.be_suffix = be->be_suffix;
3900         cfb->cb_db.be_nsuffix = be->be_nsuffix;
3901
3902         /* The suffix is always "cn=config". The underlying DB's rootdn
3903          * is always the same as the suffix.
3904          */
3905         cfb->cb_db.be_rootdn = be->be_suffix[0];
3906         cfb->cb_db.be_rootndn = be->be_nsuffix[0];
3907
3908         ber_str2bv( dir, 0, 1, &cfdir );
3909
3910         c.be = &cfb->cb_db;
3911         c.fname = "slapd";
3912         c.argc = 2;
3913         argv[0] = "directory";
3914         argv[1] = (char *)dir;
3915         argv[2] = NULL;
3916         c.argv = argv;
3917         c.reply.err = 0;
3918         c.reply.msg[0] = 0;
3919         c.table = Cft_Database;
3920
3921         ct = config_find_keyword( c.be->be_cf_ocs->co_table, &c );
3922         if ( !ct )
3923                 return 1;
3924
3925         if ( config_add_vals( ct, &c ))
3926                 return 1;
3927
3928         if ( backend_startup_one( &cfb->cb_db, &c.reply ))
3929                 return 1;
3930
3931         if ( readit ) {
3932                 void *thrctx = ldap_pvt_thread_pool_context();
3933                 int prev_DN_strict;
3934
3935                 connection_fake_init( &conn, &opbuf, thrctx );
3936                 op = &opbuf.ob_op;
3937
3938                 filter.f_desc = slap_schema.si_ad_objectClass;
3939
3940                 op->o_tag = LDAP_REQ_SEARCH;
3941
3942                 op->ors_filter = &filter;
3943                 op->ors_filterstr = filterstr;
3944                 op->ors_scope = LDAP_SCOPE_SUBTREE;
3945
3946                 op->o_dn = c.be->be_rootdn;
3947                 op->o_ndn = c.be->be_rootndn;
3948
3949                 op->o_req_dn = be->be_suffix[0];
3950                 op->o_req_ndn = be->be_nsuffix[0];
3951
3952                 op->ors_tlimit = SLAP_NO_LIMIT;
3953                 op->ors_slimit = SLAP_NO_LIMIT;
3954
3955                 op->ors_attrs = slap_anlist_all_attributes;
3956                 op->ors_attrsonly = 0;
3957
3958                 op->o_callback = &cb;
3959                 sc.cfb = cfb;
3960                 sc.ca = &c;
3961                 cb.sc_private = &sc;
3962                 sc.got_frontend = 0;
3963                 sc.got_config = 0;
3964                 sc.frontend = NULL;
3965                 sc.config = NULL;
3966
3967                 op->o_bd = &cfb->cb_db;
3968                 
3969                 /* Allow unknown attrs in DNs */
3970                 prev_DN_strict = slap_DN_strict;
3971                 slap_DN_strict = 0;
3972
3973                 rc = op->o_bd->be_search( op, &rs );
3974
3975                 /* Restore normal DN validation */
3976                 slap_DN_strict = prev_DN_strict;
3977
3978                 op->o_tag = LDAP_REQ_ADD;
3979                 if ( rc == LDAP_SUCCESS && sc.frontend ) {
3980                         op->ora_e = sc.frontend;
3981                         rc = op->o_bd->be_add( op, &rs );
3982                 }
3983                 if ( rc == LDAP_SUCCESS && sc.config ) {
3984                         op->ora_e = sc.config;
3985                         rc = op->o_bd->be_add( op, &rs );
3986                 }
3987                 ldap_pvt_thread_pool_context_reset( thrctx );
3988         }
3989
3990         /* ITS#4194 - only use if it's present, or we're converting. */
3991         if ( !readit || rc == LDAP_SUCCESS )
3992                 cfb->cb_use_ldif = 1;
3993
3994         return rc;
3995 }
3996
3997 static int
3998 CfOc_cmp( const void *c1, const void *c2 ) {
3999         const ConfigOCs *co1 = c1;
4000         const ConfigOCs *co2 = c2;
4001
4002         return ber_bvcmp( co1->co_name, co2->co_name );
4003 }
4004
4005 int
4006 config_register_schema(ConfigTable *ct, ConfigOCs *ocs) {
4007         int i;
4008
4009         i = init_config_attrs( ct );
4010         if ( i ) return i;
4011
4012         /* set up the objectclasses */
4013         i = init_config_ocs( ocs );
4014         if ( i ) return i;
4015
4016         for (i=0; ocs[i].co_def; i++) {
4017                 if ( ocs[i].co_oc ) {
4018                         ocs[i].co_name = &ocs[i].co_oc->soc_cname;
4019                         if ( !ocs[i].co_table )
4020                                 ocs[i].co_table = ct;
4021                         avl_insert( &CfOcTree, &ocs[i], CfOc_cmp, avl_dup_error );
4022                 }
4023         }
4024         return 0;
4025 }
4026
4027 int
4028 read_config(const char *fname, const char *dir) {
4029         BackendDB *be;
4030         CfBackInfo *cfb;
4031         const char *cfdir, *cfname;
4032         int rc;
4033
4034         /* Setup the config backend */
4035         be = backend_db_init( "config", NULL, 0, NULL );
4036         if ( !be )
4037                 return 1;
4038
4039         cfb = be->be_private;
4040         be->be_dfltaccess = ACL_NONE;
4041
4042         /* If no .conf, or a dir was specified, setup the dir */
4043         if ( !fname || dir ) {
4044                 if ( dir ) {
4045                         /* If explicitly given, check for existence */
4046                         struct stat st;
4047
4048                         if ( stat( dir, &st ) < 0 ) {
4049                                 Debug( LDAP_DEBUG_ANY,
4050                                         "invalid config directory %s, error %d\n",
4051                                                 dir, errno, 0 );
4052                                 return 1;
4053                         }
4054                         cfdir = dir;
4055                 } else {
4056                         cfdir = SLAPD_DEFAULT_CONFIGDIR;
4057                 }
4058                 /* if fname is defaulted, try reading .d */
4059                 rc = config_setup_ldif( be, cfdir, !fname );
4060
4061                 if ( rc ) {
4062                         /* It may be OK if the base object doesn't exist yet. */
4063                         if ( rc != LDAP_NO_SUCH_OBJECT )
4064                                 return 1;
4065                         /* ITS#4194: But if dir was specified and no fname,
4066                          * then we were supposed to read the dir. Unless we're
4067                          * trying to slapadd the dir...
4068                          */
4069                         if ( dir && !fname ) {
4070                                 if ( slapMode & (SLAP_SERVER_MODE|SLAP_TOOL_READMAIN|SLAP_TOOL_READONLY))
4071                                         return 1;
4072                                 /* Assume it's slapadd with a config dir, let it continue */
4073                                 rc = 0;
4074                                 cfb->cb_got_ldif = 1;
4075                                 cfb->cb_use_ldif = 1;
4076                                 goto done;
4077                         }
4078                 }
4079
4080                 /* If we read the config from back-ldif, nothing to do here */
4081                 if ( cfb->cb_got_ldif ) {
4082                         rc = 0;
4083                         goto done;
4084                 }
4085         }
4086
4087         if ( fname )
4088                 cfname = fname;
4089         else
4090                 cfname = SLAPD_DEFAULT_CONFIGFILE;
4091
4092         rc = read_config_file(cfname, 0, NULL, config_back_cf_table);
4093
4094         if ( rc == 0 )
4095                 ber_str2bv( cfname, 0, 1, &cfb->cb_config->c_file );
4096
4097 done:
4098         if ( rc == 0 && BER_BVISNULL( &frontendDB->be_schemadn ) ) {
4099                 ber_str2bv( SLAPD_SCHEMA_DN, STRLENOF( SLAPD_SCHEMA_DN ), 1,
4100                         &frontendDB->be_schemadn );
4101                 rc = dnNormalize( 0, NULL, NULL, &frontendDB->be_schemadn, &frontendDB->be_schemandn, NULL );
4102                 if ( rc != LDAP_SUCCESS ) {
4103                         Debug(LDAP_DEBUG_ANY, "read_config: "
4104                                 "unable to normalize default schema DN \"%s\"\n",
4105                                 frontendDB->be_schemadn.bv_val, 0, 0 );
4106                         /* must not happen */
4107                         assert( 0 );
4108                 }
4109         }
4110         return rc;
4111 }
4112
4113 static int
4114 config_back_bind( Operation *op, SlapReply *rs )
4115 {
4116         if ( be_isroot_pw( op ) ) {
4117                 ber_dupbv( &op->orb_edn, be_root_dn( op->o_bd ));
4118                 /* frontend sends result */
4119                 return LDAP_SUCCESS;
4120         }
4121
4122         rs->sr_err = LDAP_INVALID_CREDENTIALS;
4123         send_ldap_result( op, rs );
4124
4125         return rs->sr_err;
4126 }
4127
4128 static int
4129 config_send( Operation *op, SlapReply *rs, CfEntryInfo *ce, int depth )
4130 {
4131         int rc = 0;
4132
4133         if ( test_filter( op, ce->ce_entry, op->ors_filter ) == LDAP_COMPARE_TRUE )
4134         {
4135                 rs->sr_attrs = op->ors_attrs;
4136                 rs->sr_entry = ce->ce_entry;
4137                 rs->sr_flags = 0;
4138                 rc = send_search_entry( op, rs );
4139                 if ( rc != LDAP_SUCCESS ) {
4140                         return rc;
4141                 }
4142         }
4143         if ( op->ors_scope == LDAP_SCOPE_SUBTREE ) {
4144                 if ( ce->ce_kids ) {
4145                         rc = config_send( op, rs, ce->ce_kids, 1 );
4146                         if ( rc ) return rc;
4147                 }
4148                 if ( depth ) {
4149                         for (ce=ce->ce_sibs; ce; ce=ce->ce_sibs) {
4150                                 rc = config_send( op, rs, ce, 0 );
4151                                 if ( rc ) break;
4152                         }
4153                 }
4154         }
4155         return rc;
4156 }
4157
4158 static ConfigTable *
4159 config_find_table( ConfigOCs **colst, int nocs, AttributeDescription *ad,
4160         ConfigArgs *ca )
4161 {
4162         int i, j;
4163
4164         for (j=0; j<nocs; j++) {
4165                 for (i=0; colst[j]->co_table[i].name; i++)
4166                         if ( colst[j]->co_table[i].ad == ad ) {
4167                                 ca->table = colst[j]->co_type;
4168                                 return &colst[j]->co_table[i];
4169                         }
4170         }
4171         return NULL;
4172 }
4173
4174 /* Sort the attributes of the entry according to the order defined
4175  * in the objectclass, with required attributes occurring before
4176  * allowed attributes. For any attributes with sequencing dependencies
4177  * (e.g., rootDN must be defined after suffix) the objectclass must
4178  * list the attributes in the desired sequence.
4179  */
4180 static void
4181 sort_attrs( Entry *e, ConfigOCs **colst, int nocs )
4182 {
4183         Attribute *a, *head = NULL, *tail = NULL, **prev;
4184         int i, j;
4185
4186         for (i=0; i<nocs; i++) {
4187                 if ( colst[i]->co_oc->soc_required ) {
4188                         AttributeType **at = colst[i]->co_oc->soc_required;
4189                         for (j=0; at[j]; j++) {
4190                                 for (a=e->e_attrs, prev=&e->e_attrs; a;
4191                                         prev = &(*prev)->a_next, a=a->a_next) {
4192                                         if ( a->a_desc == at[j]->sat_ad ) {
4193                                                 *prev = a->a_next;
4194                                                 if (!head) {
4195                                                         head = a;
4196                                                         tail = a;
4197                                                 } else {
4198                                                         tail->a_next = a;
4199                                                         tail = a;
4200                                                 }
4201                                                 break;
4202                                         }
4203                                 }
4204                         }
4205                 }
4206                 if ( colst[i]->co_oc->soc_allowed ) {
4207                         AttributeType **at = colst[i]->co_oc->soc_allowed;
4208                         for (j=0; at[j]; j++) {
4209                                 for (a=e->e_attrs, prev=&e->e_attrs; a;
4210                                         prev = &(*prev)->a_next, a=a->a_next) {
4211                                         if ( a->a_desc == at[j]->sat_ad ) {
4212                                                 *prev = a->a_next;
4213                                                 if (!head) {
4214                                                         head = a;
4215                                                         tail = a;
4216                                                 } else {
4217                                                         tail->a_next = a;
4218                                                         tail = a;
4219                                                 }
4220                                                 break;
4221                                         }
4222                                 }
4223                         }
4224                 }
4225         }
4226         if ( tail ) {
4227                 tail->a_next = e->e_attrs;
4228                 e->e_attrs = head;
4229         }
4230 }
4231
4232 static int
4233 check_vals( ConfigTable *ct, ConfigArgs *ca, void *ptr, int isAttr )
4234 {
4235         Attribute *a = NULL;
4236         AttributeDescription *ad;
4237         BerVarray vals;
4238
4239         int i, rc = 0;
4240
4241         if ( isAttr ) {
4242                 a = ptr;
4243                 ad = a->a_desc;
4244                 vals = a->a_vals;
4245         } else {
4246                 Modifications *ml = ptr;
4247                 ad = ml->sml_desc;
4248                 vals = ml->sml_values;
4249         }
4250
4251         if ( a && ( ad->ad_type->sat_flags & SLAP_AT_ORDERED_VAL )) {
4252                 rc = ordered_value_sort( a, 1 );
4253                 if ( rc ) {
4254                         snprintf(ca->cr_msg, sizeof( ca->cr_msg ), "ordered_value_sort failed on attr %s\n",
4255                                 ad->ad_cname.bv_val );
4256                         return rc;
4257                 }
4258         }
4259         for ( i=0; vals[i].bv_val; i++ ) {
4260                 ca->line = vals[i].bv_val;
4261                 if (( ad->ad_type->sat_flags & SLAP_AT_ORDERED_VAL ) &&
4262                         ca->line[0] == '{' ) {
4263                         char *idx = strchr( ca->line, '}' );
4264                         if ( idx ) ca->line = idx+1;
4265                 }
4266                 rc = config_parse_vals( ct, ca, i );
4267                 if ( rc ) {
4268                         break;
4269                 }
4270         }
4271         return rc;
4272 }
4273
4274 static int
4275 config_rename_attr( SlapReply *rs, Entry *e, struct berval *rdn,
4276         Attribute **at )
4277 {
4278         struct berval rtype, rval;
4279         Attribute *a;
4280         AttributeDescription *ad = NULL;
4281
4282         dnRdn( &e->e_name, rdn );
4283         rval.bv_val = strchr(rdn->bv_val, '=' ) + 1;
4284         rval.bv_len = rdn->bv_len - (rval.bv_val - rdn->bv_val);
4285         rtype.bv_val = rdn->bv_val;
4286         rtype.bv_len = rval.bv_val - rtype.bv_val - 1;
4287
4288         /* Find attr */
4289         slap_bv2ad( &rtype, &ad, &rs->sr_text );
4290         a = attr_find( e->e_attrs, ad );
4291         if (!a ) return LDAP_NAMING_VIOLATION;
4292         *at = a;
4293
4294         return 0;
4295 }
4296
4297 static void
4298 config_rename_kids( CfEntryInfo *ce )
4299 {
4300         CfEntryInfo *ce2;
4301         struct berval rdn, nrdn;
4302
4303         for (ce2 = ce->ce_kids; ce2; ce2 = ce2->ce_sibs) {
4304                 struct berval newdn, newndn;
4305                 dnRdn ( &ce2->ce_entry->e_name, &rdn );
4306                 dnRdn ( &ce2->ce_entry->e_nname, &nrdn );
4307                 build_new_dn( &newdn, &ce->ce_entry->e_name, &rdn, NULL );
4308                 build_new_dn( &newndn, &ce->ce_entry->e_nname, &nrdn, NULL );
4309                 free( ce2->ce_entry->e_name.bv_val );
4310                 free( ce2->ce_entry->e_nname.bv_val );
4311                 ce2->ce_entry->e_name = newdn;
4312                 ce2->ce_entry->e_nname = newndn;
4313                 config_rename_kids( ce2 );
4314         }
4315 }
4316
4317 static int
4318 config_rename_one( Operation *op, SlapReply *rs, Entry *e,
4319         CfEntryInfo *parent, Attribute *a, struct berval *newrdn,
4320         struct berval *nnewrdn, int use_ldif )
4321 {
4322         char *ptr1;
4323         int rc = 0;
4324         struct berval odn, ondn;
4325
4326         odn = e->e_name;
4327         ondn = e->e_nname;
4328         build_new_dn( &e->e_name, &parent->ce_entry->e_name, newrdn, NULL );
4329         build_new_dn( &e->e_nname, &parent->ce_entry->e_nname, nnewrdn, NULL );
4330
4331         /* Replace attr */
4332         free( a->a_vals[0].bv_val );
4333         ptr1 = strchr( newrdn->bv_val, '=' ) + 1;
4334         a->a_vals[0].bv_len = newrdn->bv_len - (ptr1 - newrdn->bv_val);
4335         a->a_vals[0].bv_val = ch_malloc( a->a_vals[0].bv_len + 1 );
4336         strcpy( a->a_vals[0].bv_val, ptr1 );
4337
4338         if ( a->a_nvals != a->a_vals ) {
4339                 free( a->a_nvals[0].bv_val );
4340                 ptr1 = strchr( nnewrdn->bv_val, '=' ) + 1;
4341                 a->a_nvals[0].bv_len = nnewrdn->bv_len - (ptr1 - nnewrdn->bv_val);
4342                 a->a_nvals[0].bv_val = ch_malloc( a->a_nvals[0].bv_len + 1 );
4343                 strcpy( a->a_nvals[0].bv_val, ptr1 );
4344         }
4345         if ( use_ldif ) {
4346                 CfBackInfo *cfb = (CfBackInfo *)op->o_bd->be_private;
4347                 BackendDB *be = op->o_bd;
4348                 slap_callback sc = { NULL, slap_null_cb, NULL, NULL }, *scp;
4349                 struct berval dn, ndn, xdn, xndn;
4350
4351                 op->o_bd = &cfb->cb_db;
4352
4353                 /* Save current rootdn; use the underlying DB's rootdn */
4354                 dn = op->o_dn;
4355                 ndn = op->o_ndn;
4356                 xdn = op->o_req_dn;
4357                 xndn = op->o_req_ndn;
4358                 op->o_dn = op->o_bd->be_rootdn;
4359                 op->o_ndn = op->o_bd->be_rootndn;
4360                 op->o_req_dn = odn;
4361                 op->o_req_ndn = ondn;
4362
4363                 scp = op->o_callback;
4364                 op->o_callback = &sc;
4365                 op->orr_newrdn = *newrdn;
4366                 op->orr_nnewrdn = *nnewrdn;
4367                 op->orr_newSup = NULL;
4368                 op->orr_nnewSup = NULL;
4369                 op->orr_deleteoldrdn = 1;
4370                 op->orr_modlist = NULL;
4371                 slap_modrdn2mods( op, rs );
4372                 slap_mods_opattrs( op, &op->orr_modlist, 1 );
4373                 rc = op->o_bd->be_modrdn( op, rs );
4374                 slap_mods_free( op->orr_modlist, 1 );
4375
4376                 op->o_bd = be;
4377                 op->o_callback = scp;
4378                 op->o_dn = dn;
4379                 op->o_ndn = ndn;
4380                 op->o_req_dn = xdn;
4381                 op->o_req_ndn = xndn;
4382         }
4383         free( odn.bv_val );
4384         free( ondn.bv_val );
4385         if ( e->e_private )
4386                 config_rename_kids( e->e_private );
4387         return rc;
4388 }
4389
4390 static int
4391 config_renumber_one( Operation *op, SlapReply *rs, CfEntryInfo *parent, 
4392         Entry *e, int idx, int tailindex, int use_ldif )
4393 {
4394         struct berval ival, newrdn, nnewrdn;
4395         struct berval rdn;
4396         Attribute *a;
4397         char ibuf[32], *ptr1, *ptr2 = NULL;
4398         int rc = 0;
4399
4400         rc = config_rename_attr( rs, e, &rdn, &a );
4401         if ( rc ) return rc;
4402
4403         ival.bv_val = ibuf;
4404         ival.bv_len = snprintf( ibuf, sizeof( ibuf ), SLAP_X_ORDERED_FMT, idx );
4405         if ( ival.bv_len >= sizeof( ibuf ) ) {
4406                 return LDAP_NAMING_VIOLATION;
4407         }
4408         
4409         newrdn.bv_len = rdn.bv_len + ival.bv_len;
4410         newrdn.bv_val = ch_malloc( newrdn.bv_len+1 );
4411
4412         if ( tailindex ) {
4413                 ptr1 = lutil_strncopy( newrdn.bv_val, rdn.bv_val, rdn.bv_len );
4414                 ptr1 = lutil_strcopy( ptr1, ival.bv_val );
4415         } else {
4416                 int xlen;
4417                 ptr2 = ber_bvchr( &rdn, '}' );
4418                 if ( ptr2 ) {
4419                         ptr2++;
4420                 } else {
4421                         ptr2 = rdn.bv_val + a->a_desc->ad_cname.bv_len + 1;
4422                 }
4423                 xlen = rdn.bv_len - (ptr2 - rdn.bv_val);
4424                 ptr1 = lutil_strncopy( newrdn.bv_val, a->a_desc->ad_cname.bv_val,
4425                         a->a_desc->ad_cname.bv_len );
4426                 *ptr1++ = '=';
4427                 ptr1 = lutil_strcopy( ptr1, ival.bv_val );
4428                 ptr1 = lutil_strncopy( ptr1, ptr2, xlen );
4429                 *ptr1 = '\0';
4430         }
4431
4432         /* Do the equivalent of ModRDN */
4433         /* Replace DN / NDN */
4434         newrdn.bv_len = ptr1 - newrdn.bv_val;
4435         rdnNormalize( 0, NULL, NULL, &newrdn, &nnewrdn, NULL );
4436         rc = config_rename_one( op, rs, e, parent, a, &newrdn, &nnewrdn, use_ldif );
4437
4438         free( nnewrdn.bv_val );
4439         free( newrdn.bv_val );
4440         return rc;
4441 }
4442
4443 static int
4444 check_name_index( CfEntryInfo *parent, ConfigType ce_type, Entry *e,
4445         SlapReply *rs, int *renum, int *ibase )
4446 {
4447         CfEntryInfo *ce;
4448         int index = -1, gotindex = 0, nsibs, rc = 0;
4449         int renumber = 0, tailindex = 0, isfrontend = 0, isconfig = 0;
4450         char *ptr1, *ptr2 = NULL;
4451         struct berval rdn;
4452
4453         if ( renum ) *renum = 0;
4454
4455         /* These entries don't get indexed/renumbered */
4456         if ( ce_type == Cft_Global ) return 0;
4457         if ( ce_type == Cft_Schema && parent->ce_type == Cft_Global ) return 0;
4458
4459         if ( ce_type == Cft_Module )
4460                 tailindex = 1;
4461
4462         /* See if the rdn has an index already */
4463         dnRdn( &e->e_name, &rdn );
4464         if ( ce_type == Cft_Database ) {
4465                 if ( !strncmp( rdn.bv_val + rdn.bv_len - STRLENOF("frontend"),
4466                                 "frontend", STRLENOF("frontend") )) 
4467                         isfrontend = 1;
4468                 else if ( !strncmp( rdn.bv_val + rdn.bv_len - STRLENOF("config"),
4469                                 "config", STRLENOF("config") )) 
4470                         isconfig = 1;
4471         }
4472         ptr1 = ber_bvchr( &e->e_name, '{' );
4473         if ( ptr1 && ptr1 < &e->e_name.bv_val[ rdn.bv_len ] ) {
4474                 char    *next;
4475                 ptr2 = strchr( ptr1, '}' );
4476                 if ( !ptr2 || ptr2 > &e->e_name.bv_val[ rdn.bv_len ] )
4477                         return LDAP_NAMING_VIOLATION;
4478                 if ( ptr2-ptr1 == 1)
4479                         return LDAP_NAMING_VIOLATION;
4480                 gotindex = 1;
4481                 index = strtol( ptr1 + 1, &next, 10 );
4482                 if ( next == ptr1 + 1 || next[ 0 ] != '}' ) {
4483                         return LDAP_NAMING_VIOLATION;
4484                 }
4485                 if ( index < 0 ) {
4486                         /* Special case, we allow -1 for the frontendDB */
4487                         if ( index != -1 || !isfrontend )
4488                                 return LDAP_NAMING_VIOLATION;
4489                 }
4490                 if ( isconfig && index != 0 ){
4491                         return LDAP_NAMING_VIOLATION;
4492                 }
4493         }
4494
4495         /* count related kids */
4496         for (nsibs=0, ce=parent->ce_kids; ce; ce=ce->ce_sibs) {
4497                 if ( ce->ce_type == ce_type ) nsibs++;
4498         }
4499
4500         /* account for -1 frontend */
4501         if ( ce_type == Cft_Database )
4502                 nsibs--;
4503
4504         if ( index != nsibs ) {
4505                 if ( gotindex ) {
4506                         if ( index < nsibs ) {
4507                                 if ( tailindex ) return LDAP_NAMING_VIOLATION;
4508                                 /* Siblings need to be renumbered */
4509                                 if ( index != -1 || !isfrontend )
4510                                         renumber = 1;
4511                         }
4512                 }
4513                 /* config DB is always "0" */
4514                 if ( isconfig && index == -1 ) {
4515                         index = 0;
4516                 }
4517                 if (( !isfrontend && index == -1 ) || ( index > nsibs ) ){
4518                         index = nsibs;
4519                 }
4520
4521                 /* just make index = nsibs */
4522                 if ( !renumber ) {
4523                         rc = config_renumber_one( NULL, rs, parent, e, index, tailindex, 0 );
4524                 }
4525         }
4526         if ( ibase ) *ibase = index;
4527         if ( renum ) *renum = renumber;
4528         return rc;
4529 }
4530
4531 static int
4532 count_oc( ObjectClass *oc, ConfigOCs ***copp, int *nocs )
4533 {
4534         ConfigOCs       co, *cop;
4535         ObjectClass     **sups;
4536
4537         co.co_name = &oc->soc_cname;
4538         cop = avl_find( CfOcTree, &co, CfOc_cmp );
4539         if ( cop ) {
4540                 int     i;
4541
4542                 /* check for duplicates */
4543                 for ( i = 0; i < *nocs; i++ ) {
4544                         if ( *copp && (*copp)[i] == cop ) {
4545                                 break;
4546                         }
4547                 }
4548
4549                 if ( i == *nocs ) {
4550                         ConfigOCs **tmp = ch_realloc( *copp, (*nocs + 1)*sizeof( ConfigOCs * ) );
4551                         if ( tmp == NULL ) {
4552                                 return -1;
4553                         }
4554                         *copp = tmp;
4555                         (*copp)[*nocs] = cop;
4556                         (*nocs)++;
4557                 }
4558         }
4559
4560         for ( sups = oc->soc_sups; sups && *sups; sups++ ) {
4561                 if ( count_oc( *sups, copp, nocs ) ) {
4562                         return -1;
4563                 }
4564         }
4565
4566         return 0;
4567 }
4568
4569 static ConfigOCs **
4570 count_ocs( Attribute *oc_at, int *nocs )
4571 {
4572         int             i;
4573         ConfigOCs       **colst = NULL;
4574
4575         *nocs = 0;
4576
4577         for ( i = 0; !BER_BVISNULL( &oc_at->a_nvals[i] ); i++ )
4578                 /* count attrs */ ;
4579
4580         for ( ; i--; ) {
4581                 ObjectClass     *oc = oc_bvfind( &oc_at->a_nvals[i] );
4582
4583                 assert( oc != NULL );
4584                 if ( count_oc( oc, &colst, nocs ) ) {
4585                         ch_free( colst );
4586                         return NULL;
4587                 }
4588         }
4589
4590         return colst;
4591 }
4592
4593 static int
4594 cfAddInclude( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
4595 {
4596         /* Leftover from RE23. Never parse this entry */
4597         return LDAP_COMPARE_TRUE;
4598 }
4599
4600 static int
4601 cfAddSchema( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
4602 {
4603         ConfigFile *cfo;
4604
4605         /* This entry is hardcoded, don't re-parse it */
4606         if ( p->ce_type == Cft_Global ) {
4607                 cfn = p->ce_private;
4608                 ca->ca_private = cfn;
4609                 return LDAP_COMPARE_TRUE;
4610         }
4611         if ( p->ce_type != Cft_Schema )
4612                 return LDAP_CONSTRAINT_VIOLATION;
4613
4614         cfn = ch_calloc( 1, sizeof(ConfigFile) );
4615         ca->ca_private = cfn;
4616         cfo = p->ce_private;
4617         cfn->c_sibs = cfo->c_kids;
4618         cfo->c_kids = cfn;
4619         return LDAP_SUCCESS;
4620 }
4621
4622 static int
4623 cfAddDatabase( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
4624 {
4625         if ( p->ce_type != Cft_Global ) {
4626                 return LDAP_CONSTRAINT_VIOLATION;
4627         }
4628         /* config must be {0}, nothing else allowed */
4629         if ( !strncmp( e->e_nname.bv_val, "olcDatabase={0}", STRLENOF("olcDatabase={0}")) &&
4630                 strncmp( e->e_nname.bv_val + STRLENOF("olcDatabase={0}"), "config,", STRLENOF("config,") )) {
4631                 return LDAP_CONSTRAINT_VIOLATION;
4632         }
4633         ca->be = frontendDB;    /* just to get past check_vals */
4634         return LDAP_SUCCESS;
4635 }
4636
4637 static int
4638 cfAddBackend( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
4639 {
4640         if ( p->ce_type != Cft_Global ) {
4641                 return LDAP_CONSTRAINT_VIOLATION;
4642         }
4643         return LDAP_SUCCESS;
4644 }
4645
4646 static int
4647 cfAddModule( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
4648 {
4649         if ( p->ce_type != Cft_Global ) {
4650                 return LDAP_CONSTRAINT_VIOLATION;
4651         }
4652         return LDAP_SUCCESS;
4653 }
4654
4655 static int
4656 cfAddOverlay( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
4657 {
4658         if ( p->ce_type != Cft_Database ) {
4659                 return LDAP_CONSTRAINT_VIOLATION;
4660         }
4661         ca->be = p->ce_be;
4662         return LDAP_SUCCESS;
4663 }
4664
4665 static void
4666 schema_destroy_one( ConfigArgs *ca, ConfigOCs **colst, int nocs,
4667         CfEntryInfo *p )
4668 {
4669         ConfigTable *ct;
4670         ConfigFile *cfo;
4671         AttributeDescription *ad;
4672         const char *text;
4673
4674         ca->valx = -1;
4675         ca->line = NULL;
4676         ca->argc = 1;
4677         if ( cfn->c_cr_head ) {
4678                 struct berval bv = BER_BVC("olcDitContentRules");
4679                 ad = NULL;
4680                 slap_bv2ad( &bv, &ad, &text );
4681                 ct = config_find_table( colst, nocs, ad, ca );
4682                 config_del_vals( ct, ca );
4683         }
4684         if ( cfn->c_oc_head ) {
4685                 struct berval bv = BER_BVC("olcObjectClasses");
4686                 ad = NULL;
4687                 slap_bv2ad( &bv, &ad, &text );
4688                 ct = config_find_table( colst, nocs, ad, ca );
4689                 config_del_vals( ct, ca );
4690         }
4691         if ( cfn->c_at_head ) {
4692                 struct berval bv = BER_BVC("olcAttributeTypes");
4693                 ad = NULL;
4694                 slap_bv2ad( &bv, &ad, &text );
4695                 ct = config_find_table( colst, nocs, ad, ca );
4696                 config_del_vals( ct, ca );
4697         }
4698         if ( cfn->c_syn_head ) {
4699                 struct berval bv = BER_BVC("olcLdapSyntaxes");
4700                 ad = NULL;
4701                 slap_bv2ad( &bv, &ad, &text );
4702                 ct = config_find_table( colst, nocs, ad, ca );
4703                 config_del_vals( ct, ca );
4704         }
4705         if ( cfn->c_om_head ) {
4706                 struct berval bv = BER_BVC("olcObjectIdentifier");
4707                 ad = NULL;
4708                 slap_bv2ad( &bv, &ad, &text );
4709                 ct = config_find_table( colst, nocs, ad, ca );
4710                 config_del_vals( ct, ca );
4711         }
4712         cfo = p->ce_private;
4713         cfo->c_kids = cfn->c_sibs;
4714         ch_free( cfn );
4715 }
4716
4717 static int
4718 config_add_oc( ConfigOCs **cop, CfEntryInfo *last, Entry *e, ConfigArgs *ca )
4719 {
4720         int             rc = LDAP_CONSTRAINT_VIOLATION;
4721         ObjectClass     **ocp;
4722
4723         if ( (*cop)->co_ldadd ) {
4724                 rc = (*cop)->co_ldadd( last, e, ca );
4725                 if ( rc != LDAP_CONSTRAINT_VIOLATION ) {
4726                         return rc;
4727                 }
4728         }
4729
4730         for ( ocp = (*cop)->co_oc->soc_sups; ocp && *ocp; ocp++ ) {
4731                 ConfigOCs       co = { 0 };
4732
4733                 co.co_name = &(*ocp)->soc_cname;
4734                 *cop = avl_find( CfOcTree, &co, CfOc_cmp );
4735                 if ( *cop == NULL ) {
4736                         return rc;
4737                 }
4738
4739                 rc = config_add_oc( cop, last, e, ca );
4740                 if ( rc != LDAP_CONSTRAINT_VIOLATION ) {
4741                         return rc;
4742                 }
4743         }
4744
4745         return rc;
4746 }
4747
4748 /* Parse an LDAP entry into config directives */
4749 static int
4750 config_add_internal( CfBackInfo *cfb, Entry *e, ConfigArgs *ca, SlapReply *rs,
4751         int *renum, Operation *op )
4752 {
4753         CfEntryInfo     *ce, *last = NULL;
4754         ConfigOCs       co, *coptr, **colst;
4755         Attribute       *a, *oc_at, *soc_at;
4756         int             i, ibase = -1, nocs, rc = 0;
4757         struct berval   pdn;
4758         ConfigTable     *ct;
4759         char            *ptr, *log_prefix = op ? op->o_log_prefix : "";
4760
4761         memset( ca, 0, sizeof(ConfigArgs));
4762
4763         /* Make sure parent exists and entry does not. But allow
4764          * Databases and Overlays to be inserted. Don't do any
4765          * auto-renumbering if manageDSAit control is present.
4766          */
4767         ce = config_find_base( cfb->cb_root, &e->e_nname, &last );
4768         if ( ce ) {
4769                 if ( ( op && op->o_managedsait ) ||
4770                         ( ce->ce_type != Cft_Database && ce->ce_type != Cft_Overlay &&
4771                           ce->ce_type != Cft_Module ) )
4772                 {
4773                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4774                                 "DN=\"%s\" already exists\n",
4775                                 log_prefix, e->e_name.bv_val, 0 );
4776                         /* global schema ignores all writes */
4777                         if ( ce->ce_type == Cft_Schema && ce->ce_parent->ce_type == Cft_Global )
4778                                 return LDAP_COMPARE_TRUE;
4779                         return LDAP_ALREADY_EXISTS;
4780                 }
4781         }
4782
4783         dnParent( &e->e_nname, &pdn );
4784
4785         /* If last is NULL, the new entry is the root/suffix entry, 
4786          * otherwise last should be the parent.
4787          */
4788         if ( last && !dn_match( &last->ce_entry->e_nname, &pdn ) ) {
4789                 if ( rs ) {
4790                         rs->sr_matched = last->ce_entry->e_name.bv_val;
4791                 }
4792                 Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4793                         "DN=\"%s\" not child of DN=\"%s\"\n",
4794                         log_prefix, e->e_name.bv_val,
4795                         last->ce_entry->e_name.bv_val );
4796                 return LDAP_NO_SUCH_OBJECT;
4797         }
4798
4799         if ( op ) {
4800                 /* No parent, must be root. This will never happen... */
4801                 if ( !last && !be_isroot( op ) && !be_shadow_update( op ) ) {
4802                         return LDAP_NO_SUCH_OBJECT;
4803                 }
4804
4805                 if ( last && !access_allowed( op, last->ce_entry,
4806                         slap_schema.si_ad_children, NULL, ACL_WADD, NULL ) )
4807                 {
4808                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4809                                 "DN=\"%s\" no write access to \"children\" of parent\n",
4810                                 log_prefix, e->e_name.bv_val, 0 );
4811                         return LDAP_INSUFFICIENT_ACCESS;
4812                 }
4813         }
4814
4815         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
4816         if ( !oc_at ) {
4817                 Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4818                         "DN=\"%s\" no objectClass\n",
4819                         log_prefix, e->e_name.bv_val, 0 );
4820                 return LDAP_OBJECT_CLASS_VIOLATION;
4821         }
4822
4823         soc_at = attr_find( e->e_attrs, slap_schema.si_ad_structuralObjectClass );
4824         if ( !soc_at ) {
4825                 ObjectClass     *soc = NULL;
4826                 char            textbuf[ SLAP_TEXT_BUFLEN ];
4827                 const char      *text = textbuf;
4828
4829                 /* FIXME: check result */
4830                 rc = structural_class( oc_at->a_nvals, &soc, NULL,
4831                         &text, textbuf, sizeof(textbuf), NULL );
4832                 if ( rc != LDAP_SUCCESS ) {
4833                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4834                                 "DN=\"%s\" no structural objectClass (%s)\n",
4835                                 log_prefix, e->e_name.bv_val, text );
4836                         return rc;
4837                 }
4838                 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass, &soc->soc_cname, NULL );
4839                 soc_at = attr_find( e->e_attrs, slap_schema.si_ad_structuralObjectClass );
4840                 if ( soc_at == NULL ) {
4841                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4842                                 "DN=\"%s\" no structural objectClass; "
4843                                 "unable to merge computed class %s\n",
4844                                 log_prefix, e->e_name.bv_val,
4845                                 soc->soc_cname.bv_val );
4846                         return LDAP_OBJECT_CLASS_VIOLATION;
4847                 }
4848
4849                 Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4850                         "DN=\"%s\" no structural objectClass; "
4851                         "computed objectClass %s merged\n",
4852                         log_prefix, e->e_name.bv_val,
4853                         soc->soc_cname.bv_val );
4854         }
4855
4856         /* Fake the coordinates based on whether we're part of an
4857          * LDAP Add or if reading the config dir
4858          */
4859         if ( rs ) {
4860                 ca->fname = "slapd";
4861                 ca->lineno = 0;
4862         } else {
4863                 ca->fname = cfdir.bv_val;
4864                 ca->lineno = 1;
4865         }
4866         ca->ca_op = op;
4867
4868         co.co_name = &soc_at->a_nvals[0];
4869         coptr = avl_find( CfOcTree, &co, CfOc_cmp );
4870         if ( coptr == NULL ) {
4871                 Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4872                         "DN=\"%s\" no structural objectClass in configuration table\n",
4873                         log_prefix, e->e_name.bv_val, 0 );
4874                 return LDAP_OBJECT_CLASS_VIOLATION;
4875         }
4876
4877         /* Only the root can be Cft_Global, everything else must
4878          * have a parent. Only limited nesting arrangements are allowed.
4879          */
4880         rc = LDAP_CONSTRAINT_VIOLATION;
4881         if ( coptr->co_type == Cft_Global && !last ) {
4882                 cfn = cfb->cb_config;
4883                 ca->ca_private = cfn;
4884                 ca->be = frontendDB;    /* just to get past check_vals */
4885                 rc = LDAP_SUCCESS;
4886         }
4887
4888         colst = count_ocs( oc_at, &nocs );
4889
4890         /* Check whether the Add is allowed by its parent, and do
4891          * any necessary arg setup
4892          */
4893         if ( last ) {
4894                 rc = config_add_oc( &coptr, last, e, ca );
4895                 if ( rc == LDAP_CONSTRAINT_VIOLATION ) {
4896                         for ( i = 0; i<nocs; i++ ) {
4897                                 /* Already checked these */
4898                                 if ( colst[i]->co_oc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
4899                                         continue;
4900                                 if ( colst[i]->co_ldadd &&
4901                                         ( rc = colst[i]->co_ldadd( last, e, ca ))
4902                                                 != LDAP_CONSTRAINT_VIOLATION ) {
4903                                         coptr = colst[i];
4904                                         break;
4905                                 }
4906                         }
4907                 }
4908                 if ( rc == LDAP_CONSTRAINT_VIOLATION ) {
4909                         Debug( LDAP_DEBUG_TRACE, "%s: config_add_internal: "
4910                                 "DN=\"%s\" no structural objectClass add function\n",
4911                                 log_prefix, e->e_name.bv_val, 0 );
4912                         return LDAP_OBJECT_CLASS_VIOLATION;
4913                 }
4914         }
4915
4916         /* Add the entry but don't parse it, we already have its contents */
4917         if ( rc == LDAP_COMPARE_TRUE ) {
4918                 rc = LDAP_SUCCESS;
4919                 goto ok;
4920         }
4921
4922         if ( rc != LDAP_SUCCESS )
4923                 goto done_noop;
4924
4925         /* Parse all the values and check for simple syntax errors before
4926          * performing any set actions.
4927          *
4928          * If doing an LDAPadd, check for indexed names and any necessary
4929          * renaming/renumbering. Entries that don't need indexed names are
4930          * ignored. Entries that need an indexed name and arrive without one
4931          * are assigned to the end. Entries that arrive with an index may
4932          * cause the following entries to be renumbered/bumped down.
4933          *
4934          * Note that "pseudo-indexed" entries (cn=Include{xx}, cn=Module{xx})
4935          * don't allow Adding an entry with an index that's already in use.
4936          * This is flagged as an error (LDAP_ALREADY_EXISTS) up above.
4937          *
4938          * These entries can have auto-assigned indexes (appended to the end)
4939          * but only the other types support auto-renumbering of siblings.
4940          */
4941         {
4942                 rc = check_name_index( last, coptr->co_type, e, rs, renum,
4943                         &ibase );
4944                 if ( rc ) {
4945                         goto done_noop;
4946                 }
4947                 if ( renum && *renum && coptr->co_type != Cft_Database &&
4948                         coptr->co_type != Cft_Overlay )
4949                 {
4950                         snprintf( ca->cr_msg, sizeof( ca->cr_msg ),
4951                                 "operation requires sibling renumbering" );
4952                         rc = LDAP_UNWILLING_TO_PERFORM;
4953                         goto done_noop;
4954                 }
4955         }
4956
4957         init_config_argv( ca );
4958
4959         /* Make sure we process attrs in the required order */
4960         sort_attrs( e, colst, nocs );
4961
4962         for ( a = e->e_attrs; a; a = a->a_next ) {
4963                 if ( a == oc_at ) continue;
4964                 ct = config_find_table( colst, nocs, a->a_desc, ca );
4965                 if ( !ct ) continue;    /* user data? */
4966                 rc = check_vals( ct, ca, a, 1 );
4967                 if ( rc ) goto done_noop;
4968         }
4969
4970         /* Basic syntax checks are OK. Do the actual settings. */
4971         for ( a=e->e_attrs; a; a=a->a_next ) {
4972                 if ( a == oc_at ) continue;
4973                 ct = config_find_table( colst, nocs, a->a_desc, ca );
4974                 if ( !ct ) continue;    /* user data? */
4975                 for (i=0; a->a_vals[i].bv_val; i++) {
4976                         char *iptr = NULL;
4977                         ca->valx = -1;
4978                         ca->line = a->a_vals[i].bv_val;
4979                         if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED ) {
4980                                 ptr = strchr( ca->line, '}' );
4981                                 if ( ptr ) {
4982                                         iptr = strchr( ca->line, '{' );
4983                                         ca->line = ptr+1;
4984                                 }
4985                         }
4986                         if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED_SIB ) {
4987                                 if ( iptr ) {
4988                                         ca->valx = strtol( iptr+1, NULL, 0 );
4989                                 }
4990                         } else {
4991                                 ca->valx = i;
4992                         }
4993                         rc = config_parse_add( ct, ca, i );
4994                         if ( rc ) {
4995                                 rc = LDAP_OTHER;
4996                                 goto done;
4997                         }
4998                 }
4999         }
5000 ok:
5001         /* Newly added databases and overlays need to be started up */
5002         if ( CONFIG_ONLINE_ADD( ca )) {
5003                 if ( coptr->co_type == Cft_Database ) {
5004                         rc = backend_startup_one( ca->be, &ca->reply );
5005
5006                 } else if ( coptr->co_type == Cft_Overlay ) {
5007                         if ( ca->bi->bi_db_open ) {
5008                                 BackendInfo *bi_orig = ca->be->bd_info;
5009                                 ca->be->bd_info = ca->bi;
5010                                 rc = ca->bi->bi_db_open( ca->be, &ca->reply );
5011                                 ca->be->bd_info = bi_orig;
5012                         }
5013                 } else if ( ca->cleanup ) {
5014                         rc = ca->cleanup( ca );
5015                 }
5016                 if ( rc ) {
5017                         if (ca->cr_msg[0] == '\0')
5018                                 snprintf( ca->cr_msg, sizeof( ca->cr_msg ), "<%s> failed startup", ca->argv[0] );
5019
5020                         Debug(LDAP_DEBUG_ANY, "%s: %s (%s)!\n",
5021                                 ca->log, ca->cr_msg, ca->argv[1] );
5022                         rc = LDAP_OTHER;
5023                         goto done;
5024                 }
5025         }
5026
5027         ca->valx = ibase;
5028         ce = ch_calloc( 1, sizeof(CfEntryInfo) );
5029         ce->ce_parent = last;
5030         ce->ce_entry = entry_dup( e );
5031         ce->ce_entry->e_private = ce;
5032         ce->ce_type = coptr->co_type;
5033         ce->ce_be = ca->be;
5034         ce->ce_bi = ca->bi;
5035         ce->ce_private = ca->ca_private;
5036         ca->ca_entry = ce->ce_entry;
5037         if ( !last ) {
5038                 cfb->cb_root = ce;
5039         } else if ( last->ce_kids ) {
5040                 CfEntryInfo *c2, **cprev;
5041
5042                 /* Advance to first of this type */
5043                 cprev = &last->ce_kids;
5044                 for ( c2 = *cprev; c2 && c2->ce_type < ce->ce_type; ) {
5045                         cprev = &c2->ce_sibs;
5046                         c2 = c2->ce_sibs;
5047                 }
5048                 /* Account for the (-1) frontendDB entry */
5049                 if ( ce->ce_type == Cft_Database ) {
5050                         if ( ca->be == frontendDB )
5051                                 ibase = 0;
5052                         else if ( ibase != -1 )
5053                                 ibase++;
5054                 }
5055                 /* Append */
5056                 if ( ibase < 0 ) {
5057                         for (c2 = *cprev; c2 && c2->ce_type == ce->ce_type;) {
5058                                 cprev = &c2->ce_sibs;
5059                                 c2 = c2->ce_sibs;
5060                         }
5061                 } else {
5062                 /* Insert */
5063                         int i;
5064                         for ( i=0; i<ibase; i++ ) {
5065                                 c2 = *cprev;
5066                                 cprev = &c2->ce_sibs;
5067                         }
5068                 }
5069                 ce->ce_sibs = *cprev;
5070                 *cprev = ce;
5071         } else {
5072                 last->ce_kids = ce;
5073         }
5074
5075 done:
5076         if ( rc ) {
5077                 if ( (coptr->co_type == Cft_Database) && ca->be ) {
5078                         if ( ca->be != frontendDB )
5079                                 backend_destroy_one( ca->be, 1 );
5080                 } else if ( (coptr->co_type == Cft_Overlay) && ca->bi ) {
5081                         overlay_destroy_one( ca->be, (slap_overinst *)ca->bi );
5082                 } else if ( coptr->co_type == Cft_Schema ) {
5083                         schema_destroy_one( ca, colst, nocs, last );
5084                 }
5085         }
5086 done_noop:
5087
5088         ch_free( ca->argv );
5089         if ( colst ) ch_free( colst );
5090         return rc;
5091 }
5092
5093 #define BIGTMP  10000
5094 static int
5095 config_rename_add( Operation *op, SlapReply *rs, CfEntryInfo *ce,
5096         int base, int rebase, int max, int use_ldif )
5097 {
5098         CfEntryInfo *ce2, *ce3, *cetmp = NULL, *cerem = NULL;
5099         ConfigType etype = ce->ce_type;
5100         int count = 0, rc = 0;
5101
5102         /* Reverse ce list */
5103         for (ce2 = ce->ce_sibs;ce2;ce2 = ce3) {
5104                 if (ce2->ce_type != etype) {
5105                         cerem = ce2;
5106                         break;
5107                 }
5108                 ce3 = ce2->ce_sibs;
5109                 ce2->ce_sibs = cetmp;
5110                 cetmp = ce2;
5111                 count++;
5112                 if ( max && count >= max ) {
5113                         cerem = ce3;
5114                         break;
5115                 }
5116         }
5117
5118         /* Move original to a temp name until increments are done */
5119         if ( rebase ) {
5120                 ce->ce_entry->e_private = NULL;
5121                 rc = config_renumber_one( op, rs, ce->ce_parent, ce->ce_entry,
5122                         base+BIGTMP, 0, use_ldif );
5123                 ce->ce_entry->e_private = ce;
5124         }
5125         /* start incrementing */
5126         for (ce2=cetmp; ce2; ce2=ce3) {
5127                 ce3 = ce2->ce_sibs;
5128                 ce2->ce_sibs = cerem;
5129                 cerem = ce2;
5130                 if ( rc == 0 ) 
5131                         rc = config_renumber_one( op, rs, ce2->ce_parent, ce2->ce_entry,
5132                                 count+base, 0, use_ldif );
5133                 count--;
5134         }
5135         if ( rebase )
5136                 rc = config_renumber_one( op, rs, ce->ce_parent, ce->ce_entry,
5137                         base, 0, use_ldif );
5138         return rc;
5139 }
5140
5141 static int
5142 config_rename_del( Operation *op, SlapReply *rs, CfEntryInfo *ce,
5143         CfEntryInfo *ce2, int old, int use_ldif )
5144 {
5145         int count = 0;
5146
5147         /* Renumber original to a temp value */
5148         ce->ce_entry->e_private = NULL;
5149         config_renumber_one( op, rs, ce->ce_parent, ce->ce_entry,
5150                 old+BIGTMP, 0, use_ldif );
5151         ce->ce_entry->e_private = ce;
5152
5153         /* start decrementing */
5154         for (; ce2 != ce; ce2=ce2->ce_sibs) {
5155                 config_renumber_one( op, rs, ce2->ce_parent, ce2->ce_entry,
5156                         count+old, 0, use_ldif );
5157                 count++;
5158         }
5159         return config_renumber_one( op, rs, ce->ce_parent, ce->ce_entry,
5160                 count+old, 0, use_ldif );
5161 }
5162
5163 /* Parse an LDAP entry into config directives, then store in underlying
5164  * database.
5165  */
5166 static int
5167 config_back_add( Operation *op, SlapReply *rs )
5168 {
5169         CfBackInfo *cfb;
5170         int renumber;
5171         ConfigArgs ca;
5172
5173         if ( !access_allowed( op, op->ora_e, slap_schema.si_ad_entry,
5174                 NULL, ACL_WADD, NULL )) {
5175                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
5176                 goto out;
5177         }
5178
5179         /*
5180          * Check for attribute ACL
5181          */
5182         if ( !acl_check_modlist( op, op->ora_e, op->orm_modlist )) {
5183                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
5184                 rs->sr_text = "no write access to attribute";
5185                 goto out;
5186         }
5187
5188         cfb = (CfBackInfo *)op->o_bd->be_private;
5189
5190         /* add opattrs for syncprov */
5191         {
5192                 char textbuf[SLAP_TEXT_BUFLEN];
5193                 size_t textlen = sizeof textbuf;
5194                 rs->sr_err = entry_schema_check(op, op->ora_e, NULL, 0, 1, NULL,
5195                         &rs->sr_text, textbuf, sizeof( textbuf ) );
5196                 if ( rs->sr_err != LDAP_SUCCESS )
5197                         goto out;
5198                 rs->sr_err = slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
5199                 if ( rs->sr_err != LDAP_SUCCESS ) {
5200                         Debug( LDAP_DEBUG_TRACE,
5201                                 LDAP_XSTRING(config_back_add) ": entry failed op attrs add: "
5202                                 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
5203                         goto out;
5204                 }
5205         }
5206
5207         if ( op->o_abandon ) {
5208                 rs->sr_err = SLAPD_ABANDON;
5209                 goto out;
5210         }
5211         ldap_pvt_thread_pool_pause( &connection_pool );
5212
5213         /* Strategy:
5214          * 1) check for existence of entry
5215          * 2) check for sibling renumbering
5216          * 3) perform internal add
5217          * 4) perform any necessary renumbering
5218          * 5) store entry in underlying database
5219          */
5220         rs->sr_err = config_add_internal( cfb, op->ora_e, &ca, rs, &renumber, op );
5221         if ( rs->sr_err != LDAP_SUCCESS ) {
5222                 rs->sr_text = ca.cr_msg;
5223                 goto out2;
5224         }
5225
5226         if ( renumber ) {
5227                 CfEntryInfo *ce = ca.ca_entry->e_private;
5228                 req_add_s addr = op->oq_add;
5229                 op->o_tag = LDAP_REQ_MODRDN;
5230                 rs->sr_err = config_rename_add( op, rs, ce, ca.valx, 0, 0, cfb->cb_use_ldif );
5231                 op->o_tag = LDAP_REQ_ADD;
5232                 op->oq_add = addr;
5233                 if ( rs->sr_err != LDAP_SUCCESS ) {
5234                         goto out2;
5235                 }
5236         }
5237
5238         if ( cfb->cb_use_ldif ) {
5239                 BackendDB *be = op->o_bd;
5240                 slap_callback sc = { NULL, slap_null_cb, NULL, NULL }, *scp;
5241                 struct berval dn, ndn;
5242
5243                 op->o_bd = &cfb->cb_db;
5244
5245                 /* Save current rootdn; use the underlying DB's rootdn */
5246                 dn = op->o_dn;
5247                 ndn = op->o_ndn;
5248                 op->o_dn = op->o_bd->be_rootdn;
5249                 op->o_ndn = op->o_bd->be_rootndn;
5250
5251                 scp = op->o_callback;
5252                 op->o_callback = &sc;
5253                 op->o_bd->be_add( op, rs );
5254                 op->o_bd = be;
5255                 op->o_callback = scp;
5256                 op->o_dn = dn;
5257                 op->o_ndn = ndn;
5258         }
5259
5260 out2:;
5261         ldap_pvt_thread_pool_resume( &connection_pool );
5262
5263 out:;
5264         {       int repl = op->o_dont_replicate;
5265                 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
5266                         rs->sr_err = LDAP_SUCCESS;
5267                         op->o_dont_replicate = 1;
5268                 }
5269                 send_ldap_result( op, rs );
5270                 op->o_dont_replicate = repl;
5271         }
5272         slap_graduate_commit_csn( op );
5273         return rs->sr_err;
5274 }
5275
5276 typedef struct delrec {
5277         struct delrec *next;
5278         int nidx;
5279         int idx[1];
5280 } delrec;
5281
5282 static int
5283 config_modify_add( ConfigTable *ct, ConfigArgs *ca, AttributeDescription *ad,
5284         int i )
5285 {
5286         int rc;
5287
5288         ca->valx = -1;
5289         if (ad->ad_type->sat_flags & SLAP_AT_ORDERED &&
5290                 ca->line[0] == '{' )
5291         {
5292                 char *ptr = strchr( ca->line + 1, '}' );
5293                 if ( ptr ) {
5294                         char    *next;
5295
5296                         ca->valx = strtol( ca->line + 1, &next, 0 );
5297                         if ( next == ca->line + 1 || next[ 0 ] != '}' ) {
5298                                 return LDAP_OTHER;
5299                         }
5300                         ca->line = ptr+1;
5301                 }
5302         }
5303         rc = config_parse_add( ct, ca, i );
5304         if ( rc ) {
5305                 rc = LDAP_OTHER;
5306         }
5307         return rc;
5308 }
5309
5310 static int
5311 config_modify_internal( CfEntryInfo *ce, Operation *op, SlapReply *rs,
5312         ConfigArgs *ca )
5313 {
5314         int rc = LDAP_UNWILLING_TO_PERFORM;
5315         Modifications *ml;
5316         Entry *e = ce->ce_entry;
5317         Attribute *save_attrs = e->e_attrs, *oc_at, *s, *a;
5318         ConfigTable *ct;
5319         ConfigOCs **colst;
5320         int i, nocs;
5321         char *ptr;
5322         delrec *dels = NULL, *deltail = NULL;
5323
5324         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
5325         if ( !oc_at ) return LDAP_OBJECT_CLASS_VIOLATION;
5326
5327         colst = count_ocs( oc_at, &nocs );
5328
5329         /* make sure add/del flags are clear; should always be true */
5330         for ( s = save_attrs; s; s = s->a_next ) {
5331                 s->a_flags &= ~(SLAP_ATTR_IXADD|SLAP_ATTR_IXDEL);
5332         }
5333
5334         e->e_attrs = attrs_dup( e->e_attrs );
5335
5336         init_config_argv( ca );
5337         ca->be = ce->ce_be;
5338         ca->bi = ce->ce_bi;
5339         ca->ca_private = ce->ce_private;
5340         ca->ca_entry = e;
5341         ca->fname = "slapd";
5342         ca->ca_op = op;
5343         strcpy( ca->log, "back-config" );
5344
5345         for (ml = op->orm_modlist; ml; ml=ml->sml_next) {
5346                 ct = config_find_table( colst, nocs, ml->sml_desc, ca );
5347                 switch (ml->sml_op) {
5348                 case LDAP_MOD_DELETE:
5349                 case LDAP_MOD_REPLACE: {
5350                         BerVarray vals = NULL, nvals = NULL;
5351                         int *idx = NULL;
5352                         if ( ct && ( ct->arg_type & ARG_NO_DELETE )) {
5353                                 rc = LDAP_OTHER;
5354                                 snprintf(ca->cr_msg, sizeof(ca->cr_msg), "cannot delete %s",
5355                                         ml->sml_desc->ad_cname.bv_val );
5356                                 goto out_noop;
5357                         }
5358                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
5359                                 vals = ml->sml_values;
5360                                 nvals = ml->sml_nvalues;
5361                                 ml->sml_values = NULL;
5362                                 ml->sml_nvalues = NULL;
5363                         }
5364                         /* If we're deleting by values, remember the indexes of the
5365                          * values we deleted.
5366                          */
5367                         if ( ct && ml->sml_values ) {
5368                                 delrec *d;
5369                                 i = ml->sml_numvals;
5370                                 d = ch_malloc( sizeof(delrec) + (i - 1)* sizeof(int));
5371                                 d->nidx = i;
5372                                 d->next = NULL;
5373                                 if ( dels ) {
5374                                         deltail->next = d;
5375                                 } else {
5376                                         dels = d;
5377                                 }
5378                                 deltail = d;
5379                                 idx = d->idx;
5380                         }
5381                         rc = modify_delete_vindex(e, &ml->sml_mod,
5382                                 get_permissiveModify(op),
5383                                 &rs->sr_text, ca->cr_msg, sizeof(ca->cr_msg), idx );
5384                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
5385                                 ml->sml_values = vals;
5386                                 ml->sml_nvalues = nvals;
5387                         }
5388                         if ( !vals )
5389                                 break;
5390                         }
5391                         /* FALLTHRU: LDAP_MOD_REPLACE && vals */
5392
5393                 case LDAP_MOD_ADD:
5394                 case SLAP_MOD_SOFTADD: {
5395                         int mop = ml->sml_op;
5396                         int navals = -1;
5397                         ml->sml_op = LDAP_MOD_ADD;
5398                         if ( ct ) {
5399                                 if ( ct->arg_type & ARG_NO_INSERT ) {
5400                                         Attribute *a = attr_find( e->e_attrs, ml->sml_desc );
5401                                         if ( a ) {
5402                                                 navals = a->a_numvals;
5403                                         }
5404                                 }
5405                                 for ( i=0; !BER_BVISNULL( &ml->sml_values[i] ); i++ ) {
5406                                         if ( ml->sml_values[i].bv_val[0] == '{' &&
5407                                                 navals >= 0 )
5408                                         {
5409                                                 char    *next, *val = ml->sml_values[i].bv_val + 1;
5410                                                 int     j;
5411
5412                                                 j = strtol( val, &next, 0 );
5413                                                 if ( next == val || next[ 0 ] != '}' || j < navals ) {
5414                                                         rc = LDAP_OTHER;
5415                                                         snprintf(ca->cr_msg, sizeof(ca->cr_msg), "cannot insert %s",
5416                                                                 ml->sml_desc->ad_cname.bv_val );
5417                                                         goto out_noop;
5418                                                 }
5419                                         }
5420                                         rc = check_vals( ct, ca, ml, 0 );
5421                                         if ( rc ) goto out_noop;
5422                                 }
5423                         }
5424                         rc = modify_add_values(e, &ml->sml_mod,
5425                                    get_permissiveModify(op),
5426                                    &rs->sr_text, ca->cr_msg, sizeof(ca->cr_msg) );
5427
5428                         /* If value already exists, show success here
5429                          * and ignore this operation down below.
5430                          */
5431                         if ( mop == SLAP_MOD_SOFTADD ) {
5432                                 if ( rc == LDAP_TYPE_OR_VALUE_EXISTS )
5433                                         rc = LDAP_SUCCESS;
5434                                 else
5435                                         mop = LDAP_MOD_ADD;
5436                         }
5437                         ml->sml_op = mop;
5438                         break;
5439                         }
5440
5441                         break;
5442                 case LDAP_MOD_INCREMENT:        /* FIXME */
5443                         break;
5444                 default:
5445                         break;
5446                 }
5447                 if(rc != LDAP_SUCCESS) break;
5448         }
5449         
5450         if ( rc == LDAP_SUCCESS) {
5451                 /* check that the entry still obeys the schema */
5452                 rc = entry_schema_check(op, e, NULL, 0, 0, NULL,
5453                         &rs->sr_text, ca->cr_msg, sizeof(ca->cr_msg) );
5454         }
5455         if ( rc ) goto out_noop;
5456
5457         /* Basic syntax checks are OK. Do the actual settings. */
5458         for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
5459                 ct = config_find_table( colst, nocs, ml->sml_desc, ca );
5460                 if ( !ct ) continue;
5461
5462                 s = attr_find( save_attrs, ml->sml_desc );
5463                 a = attr_find( e->e_attrs, ml->sml_desc );
5464
5465                 switch (ml->sml_op) {
5466                 case LDAP_MOD_DELETE:
5467                 case LDAP_MOD_REPLACE: {
5468                         BerVarray vals = NULL, nvals = NULL;
5469                         delrec *d = NULL;
5470
5471                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
5472                                 vals = ml->sml_values;
5473                                 nvals = ml->sml_nvalues;
5474                                 ml->sml_values = NULL;
5475                                 ml->sml_nvalues = NULL;
5476                         }
5477
5478                         if ( ml->sml_values )
5479                                 d = dels;
5480
5481                         /* If we didn't delete the whole attribute */
5482                         if ( ml->sml_values && a ) {
5483                                 struct berval *mvals;
5484                                 int j;
5485
5486                                 if ( ml->sml_nvalues )
5487                                         mvals = ml->sml_nvalues;
5488                                 else
5489                                         mvals = ml->sml_values;
5490
5491                                 /* use the indexes we saved up above */
5492                                 for (i=0; i < d->nidx; i++) {
5493                                         struct berval bv = *mvals++;
5494                                         if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED &&
5495                                                 bv.bv_val[0] == '{' ) {
5496                                                 ptr = strchr( bv.bv_val, '}' ) + 1;
5497                                                 bv.bv_len -= ptr - bv.bv_val;
5498                                                 bv.bv_val = ptr;
5499                                         }
5500                                         ca->line = bv.bv_val;
5501                                         ca->valx = d->idx[i];
5502                                         config_parse_vals(ct, ca, d->idx[i] );
5503                                         rc = config_del_vals( ct, ca );
5504                                         if ( rc != LDAP_SUCCESS ) break;
5505                                         if ( s )
5506                                                 s->a_flags |= SLAP_ATTR_IXDEL;
5507                                         for (j=i+1; j < d->nidx; j++)
5508                                                 if ( d->idx[j] >d->idx[i] )
5509                                                         d->idx[j]--;
5510                                 }
5511                         } else {
5512                                 ca->valx = -1;
5513                                 ca->line = NULL;
5514                                 ca->argc = 1;
5515                                 rc = config_del_vals( ct, ca );
5516                                 if ( rc ) rc = LDAP_OTHER;
5517                                 if ( s )
5518                                         s->a_flags |= SLAP_ATTR_IXDEL;
5519                         }
5520                         if ( ml->sml_values ) {
5521                                 d = d->next;
5522                                 ch_free( dels );
5523                                 dels = d;
5524                         }
5525                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
5526                                 ml->sml_values = vals;
5527                                 ml->sml_nvalues = nvals;
5528                         }
5529                         if ( !vals || rc != LDAP_SUCCESS )
5530                                 break;
5531                         }
5532                         /* FALLTHRU: LDAP_MOD_REPLACE && vals */
5533
5534                 case LDAP_MOD_ADD:
5535                         for (i=0; ml->sml_values[i].bv_val; i++) {
5536                                 ca->line = ml->sml_values[i].bv_val;
5537                                 ca->valx = -1;
5538                                 rc = config_modify_add( ct, ca, ml->sml_desc, i );
5539                                 if ( rc )
5540                                         goto out;
5541                                 a->a_flags |= SLAP_ATTR_IXADD;
5542                         }
5543                         break;
5544                 }
5545         }
5546
5547 out:
5548         /* Undo for a failed operation */
5549         if ( rc != LDAP_SUCCESS ) {
5550                 ConfigReply msg = ca->reply;
5551                 for ( s = save_attrs; s; s = s->a_next ) {
5552                         if ( s->a_flags & SLAP_ATTR_IXDEL ) {
5553                                 s->a_flags &= ~(SLAP_ATTR_IXDEL|SLAP_ATTR_IXADD);
5554                                 ct = config_find_table( colst, nocs, s->a_desc, ca );
5555                                 a = attr_find( e->e_attrs, s->a_desc );
5556                                 if ( a ) {
5557                                         /* clear the flag so the add check below will skip it */
5558                                         a->a_flags &= ~(SLAP_ATTR_IXDEL|SLAP_ATTR_IXADD);
5559                                         ca->valx = -1;
5560                                         ca->line = NULL;
5561                                         ca->argc = 1;
5562                                         config_del_vals( ct, ca );
5563                                 }
5564                                 for ( i=0; !BER_BVISNULL( &s->a_vals[i] ); i++ ) {
5565                                         ca->line = s->a_vals[i].bv_val;
5566                                         ca->valx = -1;
5567                                         config_modify_add( ct, ca, s->a_desc, i );
5568                                 }
5569                         }
5570                 }
5571                 for ( a = e->e_attrs; a; a = a->a_next ) {
5572                         if ( a->a_flags & SLAP_ATTR_IXADD ) {
5573                                 ct = config_find_table( colst, nocs, a->a_desc, ca );
5574                                 ca->valx = -1;
5575                                 ca->line = NULL;
5576                                 ca->argc = 1;
5577                                 config_del_vals( ct, ca );
5578                                 s = attr_find( save_attrs, a->a_desc );
5579                                 if ( s ) {
5580                                         s->a_flags &= ~(SLAP_ATTR_IXDEL|SLAP_ATTR_IXADD);
5581                                         for ( i=0; !BER_BVISNULL( &s->a_vals[i] ); i++ ) {
5582                                                 ca->line = s->a_vals[i].bv_val;
5583                                                 ca->valx = -1;
5584                                                 config_modify_add( ct, ca, s->a_desc, i );
5585                                         }
5586                                 }
5587                         }
5588                 }
5589                 ca->reply = msg;
5590         }
5591
5592         if ( ca->cleanup )
5593                 ca->cleanup( ca );
5594 out_noop:
5595         if ( rc == LDAP_SUCCESS ) {
5596                 attrs_free( save_attrs );
5597         } else {
5598                 attrs_free( e->e_attrs );
5599                 e->e_attrs = save_attrs;
5600         }
5601         ch_free( ca->argv );
5602         if ( colst ) ch_free( colst );
5603         while( dels ) {
5604                 deltail = dels->next;
5605                 ch_free( dels );
5606                 dels = deltail;
5607         }
5608
5609         return rc;
5610 }
5611
5612 static int
5613 config_back_modify( Operation *op, SlapReply *rs )
5614 {
5615         CfBackInfo *cfb;
5616         CfEntryInfo *ce, *last;
5617         Modifications *ml;
5618         ConfigArgs ca = {0};
5619         struct berval rdn;
5620         char *ptr;
5621         AttributeDescription *rad = NULL;
5622         int do_pause = 1;
5623
5624         cfb = (CfBackInfo *)op->o_bd->be_private;
5625
5626         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
5627         if ( !ce ) {
5628                 if ( last )
5629                         rs->sr_matched = last->ce_entry->e_name.bv_val;
5630                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
5631                 goto out;
5632         }
5633
5634         if ( !acl_check_modlist( op, ce->ce_entry, op->orm_modlist )) {
5635                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
5636                 goto out;
5637         }
5638
5639         /* Get type of RDN */
5640         rdn = ce->ce_entry->e_nname;
5641         ptr = strchr( rdn.bv_val, '=' );
5642         rdn.bv_len = ptr - rdn.bv_val;
5643         slap_bv2ad( &rdn, &rad, &rs->sr_text );
5644
5645         /* Some basic validation... */
5646         for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
5647                 /* Don't allow Modify of RDN; must use ModRdn for that. */
5648                 if ( ml->sml_desc == rad ) {
5649                         rs->sr_err = LDAP_NOT_ALLOWED_ON_RDN;
5650                         rs->sr_text = "Use modrdn to change the entry name";
5651                         goto out;
5652                 }
5653                 /* Internal update of contextCSN? */
5654                 if ( ml->sml_desc == slap_schema.si_ad_contextCSN && op->o_conn->c_conn_idx == -1 ) {
5655                         do_pause = 0;
5656                         break;
5657                 }
5658         }
5659
5660         slap_mods_opattrs( op, &op->orm_modlist, 1 );
5661
5662         if ( do_pause ) {
5663                 if ( op->o_abandon ) {
5664                         rs->sr_err = SLAPD_ABANDON;
5665                         goto out;
5666                 }
5667                 ldap_pvt_thread_pool_pause( &connection_pool );
5668         }
5669
5670         /* Strategy:
5671          * 1) perform the Modify on the cached Entry.
5672          * 2) verify that the Entry still satisfies the schema.
5673          * 3) perform the individual config operations.
5674          * 4) store Modified entry in underlying LDIF backend.
5675          */
5676         rs->sr_err = config_modify_internal( ce, op, rs, &ca );
5677         if ( rs->sr_err ) {
5678                 rs->sr_text = ca.cr_msg;
5679         } else if ( cfb->cb_use_ldif ) {
5680                 BackendDB *be = op->o_bd;
5681                 slap_callback sc = { NULL, slap_null_cb, NULL, NULL }, *scp;
5682                 struct berval dn, ndn;
5683
5684                 op->o_bd = &cfb->cb_db;
5685
5686                 dn = op->o_dn;
5687                 ndn = op->o_ndn;
5688                 op->o_dn = op->o_bd->be_rootdn;
5689                 op->o_ndn = op->o_bd->be_rootndn;
5690
5691                 scp = op->o_callback;
5692                 op->o_callback = &sc;
5693                 op->o_bd->be_modify( op, rs );
5694                 op->o_bd = be;
5695                 op->o_callback = scp;
5696                 op->o_dn = dn;
5697                 op->o_ndn = ndn;
5698         }
5699
5700         if ( do_pause )
5701                 ldap_pvt_thread_pool_resume( &connection_pool );
5702 out:
5703         send_ldap_result( op, rs );
5704         slap_graduate_commit_csn( op );
5705         return rs->sr_err;
5706 }
5707
5708 static int
5709 config_back_modrdn( Operation *op, SlapReply *rs )
5710 {
5711         CfBackInfo *cfb;
5712         CfEntryInfo *ce, *last;
5713         struct berval rdn;
5714         int ixold, ixnew;
5715
5716         cfb = (CfBackInfo *)op->o_bd->be_private;
5717
5718         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
5719         if ( !ce ) {
5720                 if ( last )
5721                         rs->sr_matched = last->ce_entry->e_name.bv_val;
5722                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
5723                 goto out;
5724         }
5725         if ( !access_allowed( op, ce->ce_entry, slap_schema.si_ad_entry,
5726                 NULL, ACL_WRITE, NULL )) {
5727                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
5728                 goto out;
5729         }
5730         { Entry *parent;
5731                 if ( ce->ce_parent )
5732                         parent = ce->ce_parent->ce_entry;
5733                 else
5734                         parent = (Entry *)&slap_entry_root;
5735                 if ( !access_allowed( op, parent, slap_schema.si_ad_children,
5736                         NULL, ACL_WRITE, NULL )) {
5737                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
5738                         goto out;
5739                 }
5740         }
5741
5742         /* We don't allow moving objects to new parents.
5743          * Generally we only allow reordering a set of ordered entries.
5744          */
5745         if ( op->orr_newSup ) {
5746                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5747                 goto out;
5748         }
5749
5750         /* If newRDN == oldRDN, quietly succeed */
5751         dnRdn( &op->o_req_ndn, &rdn );
5752         if ( dn_match( &rdn, &op->orr_nnewrdn )) {
5753                 rs->sr_err = LDAP_SUCCESS;
5754                 goto out;
5755         }
5756
5757         /* Current behavior, subject to change as needed:
5758          *
5759          * For backends and overlays, we only allow renumbering.
5760          * For schema, we allow renaming with the same number.
5761          * Otherwise, the op is not allowed.
5762          */
5763
5764         if ( ce->ce_type == Cft_Schema ) {
5765                 char *ptr1, *ptr2;
5766                 int len;
5767
5768                 /* Can't alter the main cn=schema entry */
5769                 if ( ce->ce_parent->ce_type == Cft_Global ) {
5770                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5771                         rs->sr_text = "renaming not allowed for this entry";
5772                         goto out;
5773                 }
5774
5775                 /* We could support this later if desired */
5776                 ptr1 = ber_bvchr( &rdn, '}' );
5777                 ptr2 = ber_bvchr( &op->orr_newrdn, '}' );
5778                 len = ptr1 - rdn.bv_val;
5779                 if ( len != ptr2 - op->orr_newrdn.bv_val ||
5780                         strncmp( rdn.bv_val, op->orr_newrdn.bv_val, len )) {
5781                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5782                         rs->sr_text = "schema reordering not supported";
5783                         goto out;
5784                 }
5785         } else if ( ce->ce_type == Cft_Database ||
5786                 ce->ce_type == Cft_Overlay ) {
5787                 char *ptr1, *ptr2, *iptr1, *iptr2;
5788                 int len1, len2;
5789
5790                 iptr2 = ber_bvchr( &op->orr_newrdn, '=' ) + 1;
5791                 if ( *iptr2 != '{' ) {
5792                         rs->sr_err = LDAP_NAMING_VIOLATION;
5793                         rs->sr_text = "new ordering index is required";
5794                         goto out;
5795                 }
5796                 iptr2++;
5797                 iptr1 = ber_bvchr( &rdn, '{' ) + 1;
5798                 ptr1 = ber_bvchr( &rdn, '}' );
5799                 ptr2 = ber_bvchr( &op->orr_newrdn, '}' );
5800                 if ( !ptr2 ) {
5801                         rs->sr_err = LDAP_NAMING_VIOLATION;
5802                         rs->sr_text = "new ordering index is required";
5803                         goto out;
5804                 }
5805
5806                 len1 = ptr1 - rdn.bv_val;
5807                 len2 = ptr2 - op->orr_newrdn.bv_val;
5808
5809                 if ( rdn.bv_len - len1 != op->orr_newrdn.bv_len - len2 ||
5810                         strncmp( ptr1, ptr2, rdn.bv_len - len1 )) {
5811                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5812                         rs->sr_text = "changing database/overlay type not allowed";
5813                         goto out;
5814                 }
5815                 ixold = strtol( iptr1, NULL, 0 );
5816                 ixnew = strtol( iptr2, &ptr1, 0 );
5817                 if ( ptr1 != ptr2 || ixold < 0 || ixnew < 0 ) {
5818                         rs->sr_err = LDAP_NAMING_VIOLATION;
5819                         goto out;
5820                 }
5821                 /* config DB is always 0, cannot be changed */
5822                 if ( ce->ce_type == Cft_Database && ( ixold == 0 || ixnew == 0 )) {
5823                         rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
5824                         goto out;
5825                 }
5826         } else {
5827                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5828                 rs->sr_text = "renaming not supported for this entry";
5829                 goto out;
5830         }
5831
5832         if ( op->o_abandon ) {
5833                 rs->sr_err = SLAPD_ABANDON;
5834                 goto out;
5835         }
5836         ldap_pvt_thread_pool_pause( &connection_pool );
5837
5838         if ( ce->ce_type == Cft_Schema ) {
5839                 req_modrdn_s modr = op->oq_modrdn;
5840                 struct berval rdn;
5841                 Attribute *a;
5842                 rs->sr_err = config_rename_attr( rs, ce->ce_entry, &rdn, &a );
5843                 if ( rs->sr_err == LDAP_SUCCESS ) {
5844                         rs->sr_err = config_rename_one( op, rs, ce->ce_entry,
5845                                 ce->ce_parent, a, &op->orr_newrdn, &op->orr_nnewrdn,
5846                                 cfb->cb_use_ldif );
5847                 }
5848                 op->oq_modrdn = modr;
5849         } else {
5850                 CfEntryInfo *ce2, *cebase, **cprev, **cbprev, *ceold;
5851                 req_modrdn_s modr = op->oq_modrdn;
5852                 int i;
5853
5854                 /* Advance to first of this type */
5855                 cprev = &ce->ce_parent->ce_kids;
5856                 for ( ce2 = *cprev; ce2 && ce2->ce_type != ce->ce_type; ) {
5857                         cprev = &ce2->ce_sibs;
5858                         ce2 = ce2->ce_sibs;
5859                 }
5860                 /* Skip the -1 entry */
5861                 if ( ce->ce_type == Cft_Database ) {
5862                         cprev = &ce2->ce_sibs;
5863                         ce2 = ce2->ce_sibs;
5864                 }
5865                 cebase = ce2;
5866                 cbprev = cprev;
5867
5868                 /* Remove from old slot */
5869                 for ( ce2 = *cprev; ce2 && ce2 != ce; ce2 = ce2->ce_sibs )
5870                         cprev = &ce2->ce_sibs;
5871                 *cprev = ce->ce_sibs;
5872                 ceold = ce->ce_sibs;
5873
5874                 /* Insert into new slot */
5875                 cprev = cbprev;
5876                 for ( i=0; i<ixnew; i++ ) {
5877                         ce2 = *cprev;
5878                         if ( !ce2 )
5879                                 break;
5880                         cprev = &ce2->ce_sibs;
5881                 }
5882                 ce->ce_sibs = *cprev;
5883                 *cprev = ce;
5884
5885                 ixnew = i;
5886
5887                 /* NOTE: These should be encoded in the OC tables, not inline here */
5888                 if ( ce->ce_type == Cft_Database )
5889                         backend_db_move( ce->ce_be, ixnew );
5890                 else if ( ce->ce_type == Cft_Overlay )
5891                         overlay_move( ce->ce_be, (slap_overinst *)ce->ce_bi, ixnew );
5892                         
5893                 if ( ixold < ixnew ) {
5894                         rs->sr_err = config_rename_del( op, rs, ce, ceold, ixold,
5895                                 cfb->cb_use_ldif );
5896                 } else {
5897                         rs->sr_err = config_rename_add( op, rs, ce, ixnew, 1,
5898                                 ixold - ixnew, cfb->cb_use_ldif );
5899                 }
5900                 op->oq_modrdn = modr;
5901         }
5902
5903         ldap_pvt_thread_pool_resume( &connection_pool );
5904 out:
5905         send_ldap_result( op, rs );
5906         return rs->sr_err;
5907 }
5908
5909 static int
5910 config_back_delete( Operation *op, SlapReply *rs )
5911 {
5912 #ifdef SLAP_CONFIG_DELETE
5913         CfBackInfo *cfb;
5914         CfEntryInfo *ce, *last, *ce2;
5915
5916         cfb = (CfBackInfo *)op->o_bd->be_private;
5917
5918         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
5919         if ( !ce ) {
5920                 if ( last )
5921                         rs->sr_matched = last->ce_entry->e_name.bv_val;
5922                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
5923         } else if ( ce->ce_kids ) {
5924                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5925         } else if ( op->o_abandon ) {
5926                 rs->sr_err = SLAPD_ABANDON;
5927         } else if ( ce->ce_type == Cft_Overlay || ce->ce_type == Cft_Database ){
5928                 char *iptr;
5929                 int count, ixold;
5930
5931                 ldap_pvt_thread_pool_pause( &connection_pool );
5932
5933                 if ( ce->ce_type == Cft_Overlay ){
5934                         overlay_remove( ce->ce_be, (slap_overinst *)ce->ce_bi );
5935                 } else { /* Cft_Database*/
5936                         if ( ce->ce_be == frontendDB || ce->ce_be == op->o_bd ){
5937                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
5938                                 rs->sr_text = "Cannot delete config or frontend database";
5939                                 ldap_pvt_thread_pool_resume( &connection_pool );
5940                                 goto out;
5941                         } 
5942                         if ( ce->ce_be->bd_info->bi_db_close ) {
5943                                 ce->ce_be->bd_info->bi_db_close( ce->ce_be, NULL );
5944                         }
5945                         backend_destroy_one( ce->ce_be, 1);
5946                 }
5947
5948                 /* remove CfEntryInfo from the siblings list */
5949                 if ( ce->ce_parent->ce_kids == ce ) {
5950                         ce->ce_parent->ce_kids = ce->ce_sibs;
5951                 } else {
5952                         for ( ce2 = ce->ce_parent->ce_kids ; ce2; ce2 = ce2->ce_sibs ) {
5953                                 if ( ce2->ce_sibs == ce ) {
5954                                         ce2->ce_sibs = ce->ce_sibs;
5955                                         break;
5956                                 }
5957                         }
5958                 }
5959
5960                 /* remove from underlying database */
5961                 if ( cfb->cb_use_ldif ) {
5962                         BackendDB *be = op->o_bd;
5963                         slap_callback sc = { NULL, slap_null_cb, NULL, NULL }, *scp;
5964                         struct berval dn, ndn, req_dn, req_ndn;
5965
5966                         op->o_bd = &cfb->cb_db;
5967
5968                         dn = op->o_dn;
5969                         ndn = op->o_ndn;
5970                         req_dn = op->o_req_dn;
5971                         req_ndn = op->o_req_ndn;
5972
5973                         op->o_dn = op->o_bd->be_rootdn;
5974                         op->o_ndn = op->o_bd->be_rootndn;
5975                         op->o_req_dn = ce->ce_entry->e_name;
5976                         op->o_req_ndn = ce->ce_entry->e_nname;
5977
5978                         scp = op->o_callback;
5979                         op->o_callback = &sc;
5980                         op->o_bd->be_delete( op, rs );
5981                         op->o_bd = be;
5982                         op->o_callback = scp;
5983                         op->o_dn = dn;
5984                         op->o_ndn = ndn;
5985                         op->o_req_dn = req_dn;
5986                         op->o_req_ndn = req_ndn;
5987                 }
5988
5989                 /* renumber siblings */
5990                 iptr = ber_bvchr( &op->o_req_ndn, '{' ) + 1;
5991                 ixold = strtol( iptr, NULL, 0 );
5992                 for (ce2 = ce->ce_sibs, count=0; ce2; ce2=ce2->ce_sibs) {
5993                         config_renumber_one( op, rs, ce2->ce_parent, ce2->ce_entry,
5994                                 count+ixold, 0, cfb->cb_use_ldif );
5995                         count++;
5996                 }
5997
5998                 ce->ce_entry->e_private=NULL;
5999                 entry_free(ce->ce_entry);
6000                 ch_free(ce);
6001                 ldap_pvt_thread_pool_resume( &connection_pool );
6002         } else {
6003                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
6004         }
6005 #else
6006         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
6007 #endif /* SLAP_CONFIG_DELETE */
6008 out:
6009         send_ldap_result( op, rs );
6010         return rs->sr_err;
6011 }
6012
6013 static int
6014 config_back_search( Operation *op, SlapReply *rs )
6015 {
6016         CfBackInfo *cfb;
6017         CfEntryInfo *ce, *last;
6018         slap_mask_t mask;
6019
6020         cfb = (CfBackInfo *)op->o_bd->be_private;
6021
6022         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
6023         if ( !ce ) {
6024                 if ( last )
6025                         rs->sr_matched = last->ce_entry->e_name.bv_val;
6026                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
6027                 goto out;
6028         }
6029         if ( !access_allowed_mask( op, ce->ce_entry, slap_schema.si_ad_entry, NULL,
6030                 ACL_SEARCH, NULL, &mask ))
6031         {
6032                 if ( !ACL_GRANT( mask, ACL_DISCLOSE )) {
6033                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
6034                 } else {
6035                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
6036                 }
6037                 goto out;
6038         }
6039         switch ( op->ors_scope ) {
6040         case LDAP_SCOPE_BASE:
6041         case LDAP_SCOPE_SUBTREE:
6042                 rs->sr_err = config_send( op, rs, ce, 0 );
6043                 break;
6044                 
6045         case LDAP_SCOPE_ONELEVEL:
6046                 for (ce = ce->ce_kids; ce; ce=ce->ce_sibs) {
6047                         rs->sr_err = config_send( op, rs, ce, 1 );
6048                         if ( rs->sr_err ) {
6049                                 break;
6050                         }
6051                 }
6052                 break;
6053         }
6054
6055 out:
6056         send_ldap_result( op, rs );
6057         return rs->sr_err;
6058 }
6059
6060 /* no-op, we never free entries */
6061 int config_entry_release(
6062         Operation *op,
6063         Entry *e,
6064         int rw )
6065 {
6066         if ( !e->e_private ) {
6067                 entry_free( e );
6068         }
6069         return LDAP_SUCCESS;
6070 }
6071
6072 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
6073  */
6074 int config_back_entry_get(
6075         Operation *op,
6076         struct berval *ndn,
6077         ObjectClass *oc,
6078         AttributeDescription *at,
6079         int rw,
6080         Entry **ent )
6081 {
6082         CfBackInfo *cfb;
6083         CfEntryInfo *ce, *last;
6084         int rc = LDAP_NO_SUCH_OBJECT;
6085
6086         cfb = (CfBackInfo *)op->o_bd->be_private;
6087
6088         ce = config_find_base( cfb->cb_root, ndn, &last );
6089         if ( ce ) {
6090                 *ent = ce->ce_entry;
6091                 if ( *ent ) {
6092                         rc = LDAP_SUCCESS;
6093                         if ( oc && !is_entry_objectclass_or_sub( *ent, oc ) ) {
6094                                 rc = LDAP_NO_SUCH_ATTRIBUTE;
6095                                 *ent = NULL;
6096                         }
6097                 }
6098         }
6099
6100         return rc;
6101 }
6102
6103 static int
6104 config_build_attrs( Entry *e, AttributeType **at, AttributeDescription *ad,
6105         ConfigTable *ct, ConfigArgs *c )
6106 {
6107         int i, rc;
6108
6109         for (; at && *at; at++) {
6110                 /* Skip the naming attr */
6111                 if ((*at)->sat_ad == ad || (*at)->sat_ad == slap_schema.si_ad_cn )
6112                         continue;
6113                 for (i=0;ct[i].name;i++) {
6114                         if (ct[i].ad == (*at)->sat_ad) {
6115                                 rc = config_get_vals(&ct[i], c);
6116                                 /* NOTE: tolerate that config_get_vals()
6117                                  * returns success with no values */
6118                                 if (rc == LDAP_SUCCESS && c->rvalue_vals != NULL ) {
6119                                         if ( c->rvalue_nvals )
6120                                                 rc = attr_merge(e, ct[i].ad, c->rvalue_vals,
6121                                                         c->rvalue_nvals);
6122                                         else {
6123                                                 slap_syntax_validate_func *validate =
6124                                                         ct[i].ad->ad_type->sat_syntax->ssyn_validate;
6125                                                 if ( validate ) {
6126                                                         int j;
6127                                                         for ( j=0; c->rvalue_vals[j].bv_val; j++ ) {
6128                                                                 rc = ordered_value_validate( ct[i].ad,
6129                                                                         &c->rvalue_vals[j], LDAP_MOD_ADD );
6130                                                                 if ( rc ) {
6131                                                                         Debug( LDAP_DEBUG_ANY,
6132                                                                                 "config_build_attrs: error %d on %s value #%d\n",
6133                                                                                 rc, ct[i].ad->ad_cname.bv_val, j );
6134                                                                         return rc;
6135                                                                 }
6136                                                         }
6137                                                 }
6138                                                         
6139                                                 rc = attr_merge_normalize(e, ct[i].ad,
6140                                                         c->rvalue_vals, NULL);
6141                                         }
6142                                         ber_bvarray_free( c->rvalue_nvals );
6143                                         ber_bvarray_free( c->rvalue_vals );
6144                                         if ( rc ) {
6145                                                 Debug( LDAP_DEBUG_ANY,
6146                                                         "config_build_attrs: error %d on %s\n",
6147                                                         rc, ct[i].ad->ad_cname.bv_val, 0 );
6148                                                 return rc;
6149                                         }
6150                                 }
6151                                 break;
6152                         }
6153                 }
6154         }
6155         return 0;
6156 }
6157
6158 Entry *
6159 config_build_entry( Operation *op, SlapReply *rs, CfEntryInfo *parent,
6160         ConfigArgs *c, struct berval *rdn, ConfigOCs *main, ConfigOCs *extra )
6161 {
6162         Entry *e = entry_alloc();
6163         CfEntryInfo *ce = ch_calloc( 1, sizeof(CfEntryInfo) );
6164         struct berval val;
6165         struct berval ad_name;
6166         AttributeDescription *ad = NULL;
6167         int rc;
6168         char *ptr;
6169         const char *text = "";
6170         Attribute *oc_at;
6171         struct berval pdn;
6172         ObjectClass *oc;
6173         CfEntryInfo *ceprev = NULL;
6174
6175         Debug( LDAP_DEBUG_TRACE, "config_build_entry: \"%s\"\n", rdn->bv_val, 0, 0);
6176         e->e_private = ce;
6177         ce->ce_entry = e;
6178         ce->ce_type = main->co_type;
6179         ce->ce_parent = parent;
6180         if ( parent ) {
6181                 pdn = parent->ce_entry->e_nname;
6182                 if ( parent->ce_kids && parent->ce_kids->ce_type <= ce->ce_type )
6183                         for ( ceprev = parent->ce_kids; ceprev->ce_sibs &&
6184                                 ceprev->ce_type <= ce->ce_type;
6185                                 ceprev = ceprev->ce_sibs );
6186         } else {
6187                 BER_BVZERO( &pdn );
6188         }
6189
6190         ce->ce_private = c->ca_private;
6191         ce->ce_be = c->be;
6192         ce->ce_bi = c->bi;
6193
6194         build_new_dn( &e->e_name, &pdn, rdn, NULL );
6195         ber_dupbv( &e->e_nname, &e->e_name );
6196
6197         attr_merge_normalize_one(e, slap_schema.si_ad_objectClass,
6198                 main->co_name, NULL );
6199         if ( extra )
6200                 attr_merge_normalize_one(e, slap_schema.si_ad_objectClass,
6201                         extra->co_name, NULL );
6202         ptr = strchr(rdn->bv_val, '=');
6203         ad_name.bv_val = rdn->bv_val;
6204         ad_name.bv_len = ptr - rdn->bv_val;
6205         rc = slap_bv2ad( &ad_name, &ad, &text );
6206         if ( rc ) {
6207                 goto fail;
6208         }
6209         val.bv_val = ptr+1;
6210         val.bv_len = rdn->bv_len - (val.bv_val - rdn->bv_val);
6211         attr_merge_normalize_one(e, ad, &val, NULL );
6212
6213         oc = main->co_oc;
6214         c->table = main->co_type;
6215         if ( oc->soc_required ) {
6216                 rc = config_build_attrs( e, oc->soc_required, ad, main->co_table, c );
6217                 if ( rc ) goto fail;
6218         }
6219
6220         if ( oc->soc_allowed ) {
6221                 rc = config_build_attrs( e, oc->soc_allowed, ad, main->co_table, c );
6222                 if ( rc ) goto fail;
6223         }
6224
6225         if ( extra ) {
6226                 oc = extra->co_oc;
6227                 c->table = extra->co_type;
6228                 if ( oc->soc_required ) {
6229                         rc = config_build_attrs( e, oc->soc_required, ad, extra->co_table, c );
6230                         if ( rc ) goto fail;
6231                 }
6232
6233                 if ( oc->soc_allowed ) {
6234                         rc = config_build_attrs( e, oc->soc_allowed, ad, extra->co_table, c );
6235                         if ( rc ) goto fail;
6236                 }
6237         }
6238
6239         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
6240         rc = structural_class(oc_at->a_vals, &oc, NULL, &text, c->cr_msg,
6241                 sizeof(c->cr_msg), op ? op->o_tmpmemctx : NULL );
6242         if ( rc != LDAP_SUCCESS ) {
6243 fail:
6244                 Debug( LDAP_DEBUG_ANY,
6245                         "config_build_entry: build \"%s\" failed: \"%s\"\n",
6246                         rdn->bv_val, text, 0);
6247                 return NULL;
6248         }
6249         attr_merge_normalize_one(e, slap_schema.si_ad_structuralObjectClass, &oc->soc_cname, NULL );
6250         if ( op ) {
6251                 op->ora_e = e;
6252                 op->ora_modlist = NULL;
6253                 slap_add_opattrs( op, NULL, NULL, 0, 0 );
6254                 if ( !op->o_noop ) {
6255                         op->o_bd->be_add( op, rs );
6256                         if ( ( rs->sr_err != LDAP_SUCCESS ) 
6257                                         && (rs->sr_err != LDAP_ALREADY_EXISTS) ) {
6258                                 goto fail;
6259                         }
6260                 }
6261         }
6262         if ( ceprev ) {
6263                 ce->ce_sibs = ceprev->ce_sibs;
6264                 ceprev->ce_sibs = ce;
6265         } else if ( parent ) {
6266                 ce->ce_sibs = parent->ce_kids;
6267                 parent->ce_kids = ce;
6268         }
6269
6270         return e;
6271 }
6272
6273 static int
6274 config_build_schema_inc( ConfigArgs *c, CfEntryInfo *ceparent,
6275         Operation *op, SlapReply *rs )
6276 {
6277         Entry *e;
6278         ConfigFile *cf = c->ca_private;
6279         char *ptr;
6280         struct berval bv, rdn;
6281
6282         for (; cf; cf=cf->c_sibs, c->depth++) {
6283                 if ( !cf->c_at_head && !cf->c_cr_head && !cf->c_oc_head &&
6284                         !cf->c_om_head && !cf->c_syn_head ) continue;
6285                 c->value_dn.bv_val = c->log;
6286                 LUTIL_SLASHPATH( cf->c_file.bv_val );
6287                 bv.bv_val = strrchr(cf->c_file.bv_val, LDAP_DIRSEP[0]);
6288                 if ( !bv.bv_val ) {
6289                         bv = cf->c_file;
6290                 } else {
6291                         bv.bv_val++;
6292                         bv.bv_len = cf->c_file.bv_len - (bv.bv_val - cf->c_file.bv_val);
6293                 }
6294                 ptr = strchr( bv.bv_val, '.' );
6295                 if ( ptr )
6296                         bv.bv_len = ptr - bv.bv_val;
6297                 c->value_dn.bv_len = snprintf(c->value_dn.bv_val, sizeof( c->log ), "cn=" SLAP_X_ORDERED_FMT, c->depth);
6298                 if ( c->value_dn.bv_len >= sizeof( c->log ) ) {
6299                         /* FIXME: how can indicate error? */
6300                         return -1;
6301                 }
6302                 strncpy( c->value_dn.bv_val + c->value_dn.bv_len, bv.bv_val,
6303                         bv.bv_len );
6304                 c->value_dn.bv_len += bv.bv_len;
6305                 c->value_dn.bv_val[c->value_dn.bv_len] ='\0';
6306                 rdn = c->value_dn;
6307
6308                 c->ca_private = cf;
6309                 e = config_build_entry( op, rs, ceparent, c, &rdn,
6310                         &CFOC_SCHEMA, NULL );
6311                 if ( !e ) {
6312                         return -1;
6313                 } else if ( e && cf->c_kids ) {
6314                         c->ca_private = cf->c_kids;
6315                         config_build_schema_inc( c, e->e_private, op, rs );
6316                 }
6317         }
6318         return 0;
6319 }
6320
6321 #ifdef SLAPD_MODULES
6322
6323 static int
6324 config_build_modules( ConfigArgs *c, CfEntryInfo *ceparent,
6325         Operation *op, SlapReply *rs )
6326 {
6327         int i;
6328         ModPaths *mp;
6329
6330         for (i=0, mp=&modpaths; mp; mp=mp->mp_next, i++) {
6331                 if ( BER_BVISNULL( &mp->mp_path ) && !mp->mp_loads )
6332                         continue;
6333                 c->value_dn.bv_val = c->log;
6334                 c->value_dn.bv_len = snprintf(c->value_dn.bv_val, sizeof( c->log ), "cn=module" SLAP_X_ORDERED_FMT, i);
6335                 if ( c->value_dn.bv_len >= sizeof( c->log ) ) {
6336                         /* FIXME: how can indicate error? */
6337                         return -1;
6338                 }
6339                 c->ca_private = mp;
6340                 if ( ! config_build_entry( op, rs, ceparent, c, &c->value_dn, &CFOC_MODULE, NULL )) {
6341                         return -1;
6342                 }
6343         }
6344         return 0;
6345 }
6346 #endif
6347
6348 static int
6349 config_check_schema(Operation *op, CfBackInfo *cfb)
6350 {
6351         struct berval schema_dn = BER_BVC(SCHEMA_RDN "," CONFIG_RDN);
6352         ConfigArgs c = {0};
6353         CfEntryInfo *ce, *last;
6354         Entry *e;
6355
6356         /* If there's no root entry, we must be in the midst of converting */
6357         if ( !cfb->cb_root )
6358                 return 0;
6359
6360         /* Make sure the main schema entry exists */
6361         ce = config_find_base( cfb->cb_root, &schema_dn, &last );
6362         if ( ce ) {
6363                 Attribute *a;
6364                 struct berval *bv;
6365
6366                 e = ce->ce_entry;
6367
6368                 /* Make sure it's up to date */
6369                 if ( cf_om_tail != om_sys_tail ) {
6370                         a = attr_find( e->e_attrs, cfAd_om );
6371                         if ( a ) {
6372                                 if ( a->a_nvals != a->a_vals )
6373                                         ber_bvarray_free( a->a_nvals );
6374                                 ber_bvarray_free( a->a_vals );
6375                                 a->a_vals = NULL;
6376                                 a->a_nvals = NULL;
6377                                 a->a_numvals = 0;
6378                         }
6379                         oidm_unparse( &bv, NULL, NULL, 1 );
6380                         attr_merge_normalize( e, cfAd_om, bv, NULL );
6381                         ber_bvarray_free( bv );
6382                         cf_om_tail = om_sys_tail;
6383                 }
6384                 if ( cf_at_tail != at_sys_tail ) {
6385                         a = attr_find( e->e_attrs, cfAd_attr );
6386                         if ( a ) {
6387                                 if ( a->a_nvals != a->a_vals )
6388                                         ber_bvarray_free( a->a_nvals );
6389                                 ber_bvarray_free( a->a_vals );
6390                                 a->a_vals = NULL;
6391                                 a->a_nvals = NULL;
6392                                 a->a_numvals = 0;
6393                         }
6394                         at_unparse( &bv, NULL, NULL, 1 );
6395                         attr_merge_normalize( e, cfAd_attr, bv, NULL );
6396                         ber_bvarray_free( bv );
6397                         cf_at_tail = at_sys_tail;
6398                 }
6399                 if ( cf_oc_tail != oc_sys_tail ) {
6400                         a = attr_find( e->e_attrs, cfAd_oc );
6401                         if ( a ) {
6402                                 if ( a->a_nvals != a->a_vals )
6403                                         ber_bvarray_free( a->a_nvals );
6404                                 ber_bvarray_free( a->a_vals );
6405                                 a->a_vals = NULL;
6406                                 a->a_nvals = NULL;
6407                                 a->a_numvals = 0;
6408                         }
6409                         oc_unparse( &bv, NULL, NULL, 1 );
6410                         attr_merge_normalize( e, cfAd_oc, bv, NULL );
6411                         ber_bvarray_free( bv );
6412                         cf_oc_tail = oc_sys_tail;
6413                 }
6414                 if ( cf_syn_tail != syn_sys_tail ) {
6415                         a = attr_find( e->e_attrs, cfAd_syntax );
6416                         if ( a ) {
6417                                 if ( a->a_nvals != a->a_vals )
6418                                         ber_bvarray_free( a->a_nvals );
6419                                 ber_bvarray_free( a->a_vals );
6420                                 a->a_vals = NULL;
6421                                 a->a_nvals = NULL;
6422                                 a->a_numvals = 0;
6423                         }
6424                         syn_unparse( &bv, NULL, NULL, 1 );
6425                         attr_merge_normalize( e, cfAd_syntax, bv, NULL );
6426                         ber_bvarray_free( bv );
6427                         cf_syn_tail = syn_sys_tail;
6428                 }
6429         } else {
6430                 SlapReply rs = {REP_RESULT};
6431                 c.ca_private = NULL;
6432                 e = config_build_entry( op, &rs, cfb->cb_root, &c, &schema_rdn,
6433                         &CFOC_SCHEMA, NULL );
6434                 if ( !e ) {
6435                         return -1;
6436                 }
6437                 ce = e->e_private;
6438                 ce->ce_private = cfb->cb_config;
6439                 cf_at_tail = at_sys_tail;
6440                 cf_oc_tail = oc_sys_tail;
6441                 cf_om_tail = om_sys_tail;
6442                 cf_syn_tail = syn_sys_tail;
6443         }
6444         return 0;
6445 }
6446
6447 static const char *defacl[] = {
6448         NULL, "to", "*", "by", "*", "none", NULL
6449 };
6450
6451 static int
6452 config_back_db_open( BackendDB *be, ConfigReply *cr )
6453 {
6454         CfBackInfo *cfb = be->be_private;
6455         struct berval rdn;
6456         Entry *e, *parent;
6457         CfEntryInfo *ce, *ceparent;
6458         int i, unsupp = 0;
6459         BackendInfo *bi;
6460         ConfigArgs c;
6461         Connection conn = {0};
6462         OperationBuffer opbuf;
6463         Operation *op;
6464         slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
6465         SlapReply rs = {REP_RESULT};
6466         void *thrctx = NULL;
6467
6468         Debug( LDAP_DEBUG_TRACE, "config_back_db_open\n", 0, 0, 0);
6469
6470         /* If we have no explicitly configured ACLs, don't just use
6471          * the global ACLs. Explicitly deny access to everything.
6472          */
6473         if ( !be->be_acl ) {
6474                 parse_acl(be, "config_back_db_open", 0, 6, (char **)defacl, 0 );
6475         }
6476
6477         thrctx = ldap_pvt_thread_pool_context();
6478         connection_fake_init( &conn, &opbuf, thrctx );
6479         op = &opbuf.ob_op;
6480
6481         op->o_tag = LDAP_REQ_ADD;
6482         op->o_callback = &cb;
6483         op->o_bd = &cfb->cb_db;
6484         op->o_dn = op->o_bd->be_rootdn;
6485         op->o_ndn = op->o_bd->be_rootndn;
6486
6487         if ( !cfb->cb_use_ldif ) {
6488                 op->o_noop = 1;
6489         }
6490
6491         /* If we read the config from back-ldif, do some quick sanity checks */
6492         if ( cfb->cb_got_ldif ) {
6493                 return config_check_schema( op, cfb );
6494         }
6495
6496         /* create root of tree */
6497         rdn = config_rdn;
6498         c.ca_private = cfb->cb_config;
6499         c.be = frontendDB;
6500         e = config_build_entry( op, &rs, NULL, &c, &rdn, &CFOC_GLOBAL, NULL );
6501         if ( !e ) {
6502                 return -1;
6503         }
6504         ce = e->e_private;
6505         cfb->cb_root = ce;
6506
6507         parent = e;
6508         ceparent = ce;
6509
6510 #ifdef SLAPD_MODULES
6511         /* Create Module nodes... */
6512         if ( modpaths.mp_loads ) {
6513                 if ( config_build_modules( &c, ceparent, op, &rs ) ){
6514                         return -1;
6515                 }
6516         }
6517 #endif
6518
6519         /* Create schema nodes... cn=schema will contain the hardcoded core
6520          * schema, read-only. Child objects will contain runtime loaded schema
6521          * files.
6522          */
6523         rdn = schema_rdn;
6524         c.ca_private = NULL;
6525         e = config_build_entry( op, &rs, ceparent, &c, &rdn, &CFOC_SCHEMA, NULL );
6526         if ( !e ) {
6527                 return -1;
6528         }
6529         ce = e->e_private;
6530         ce->ce_private = cfb->cb_config;
6531         cf_at_tail = at_sys_tail;
6532         cf_oc_tail = oc_sys_tail;
6533         cf_om_tail = om_sys_tail;
6534         cf_syn_tail = syn_sys_tail;
6535
6536         /* Create schema nodes for included schema... */
6537         if ( cfb->cb_config->c_kids ) {
6538                 c.depth = 0;
6539                 c.ca_private = cfb->cb_config->c_kids;
6540                 if (config_build_schema_inc( &c, ce, op, &rs )) {
6541                         return -1;
6542                 }
6543         }
6544
6545         /* Create backend nodes. Skip if they don't provide a cf_table.
6546          * There usually aren't any of these.
6547          */
6548         
6549         c.line = 0;
6550         LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next) {
6551                 if (!bi->bi_cf_ocs) {
6552                         /* If it only supports the old config mech, complain. */
6553                         if ( bi->bi_config ) {
6554                                 Debug( LDAP_DEBUG_ANY,
6555                                         "WARNING: No dynamic config support for backend %s.\n",
6556                                         bi->bi_type, 0, 0 );
6557                                 unsupp++;
6558                         }
6559                         continue;
6560                 }
6561                 if (!bi->bi_private) continue;
6562
6563                 rdn.bv_val = c.log;
6564                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( c.log ),
6565                         "%s=%s", cfAd_backend->ad_cname.bv_val, bi->bi_type);
6566                 if ( rdn.bv_len >= sizeof( c.log ) ) {
6567                         /* FIXME: holler ... */ ;
6568                 }
6569                 c.bi = bi;
6570                 e = config_build_entry( op, &rs, ceparent, &c, &rdn, &CFOC_BACKEND,
6571                         bi->bi_cf_ocs );
6572                 if ( !e ) {
6573                         return -1;
6574                 }
6575         }
6576
6577         /* Create database nodes... */
6578         frontendDB->be_cf_ocs = &CFOC_FRONTEND;
6579         LDAP_STAILQ_NEXT(frontendDB, be_next) = LDAP_STAILQ_FIRST(&backendDB);
6580         for ( i = -1, be = frontendDB ; be;
6581                 i++, be = LDAP_STAILQ_NEXT( be, be_next )) {
6582                 slap_overinfo *oi = NULL;
6583
6584                 if ( overlay_is_over( be )) {
6585                         oi = be->bd_info->bi_private;
6586                         bi = oi->oi_orig;
6587                 } else {
6588                         bi = be->bd_info;
6589                 }
6590
6591                 /* If this backend supports the old config mechanism, but not
6592                  * the new mech, complain.
6593                  */
6594                 if ( !be->be_cf_ocs && bi->bi_db_config ) {
6595                         Debug( LDAP_DEBUG_ANY,
6596                                 "WARNING: No dynamic config support for database %s.\n",
6597                                 bi->bi_type, 0, 0 );
6598                         unsupp++;
6599                 }
6600                 rdn.bv_val = c.log;
6601                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( c.log ),
6602                         "%s=" SLAP_X_ORDERED_FMT "%s", cfAd_database->ad_cname.bv_val,
6603                         i, bi->bi_type);
6604                 if ( rdn.bv_len >= sizeof( c.log ) ) {
6605                         /* FIXME: holler ... */ ;
6606                 }
6607                 c.be = be;
6608                 c.bi = bi;
6609                 e = config_build_entry( op, &rs, ceparent, &c, &rdn, &CFOC_DATABASE,
6610                         be->be_cf_ocs );
6611                 if ( !e ) {
6612                         return -1;
6613                 }
6614                 ce = e->e_private;
6615                 if ( be->be_cf_ocs && be->be_cf_ocs->co_cfadd )
6616                         be->be_cf_ocs->co_cfadd( op, &rs, e, &c );
6617                 /* Iterate through overlays */
6618                 if ( oi ) {
6619                         slap_overinst *on;
6620                         Entry *oe;
6621                         int j;
6622                         voidList *vl, *v0 = NULL;
6623
6624                         /* overlays are in LIFO order, must reverse stack */
6625                         for (on=oi->oi_list; on; on=on->on_next) {
6626                                 vl = ch_malloc( sizeof( voidList ));
6627                                 vl->vl_next = v0;
6628                                 v0 = vl;
6629                                 vl->vl_ptr = on;
6630                         }
6631                         for (j=0; vl; j++,vl=v0) {
6632                                 on = vl->vl_ptr;
6633                                 v0 = vl->vl_next;
6634                                 ch_free( vl );
6635                                 if ( on->on_bi.bi_db_config && !on->on_bi.bi_cf_ocs ) {
6636                                         Debug( LDAP_DEBUG_ANY,
6637                                                 "WARNING: No dynamic config support for overlay %s.\n",
6638                                                 on->on_bi.bi_type, 0, 0 );
6639                                         unsupp++;
6640                                 }
6641                                 rdn.bv_val = c.log;
6642                                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( c.log ),
6643                                         "%s=" SLAP_X_ORDERED_FMT "%s",
6644                                         cfAd_overlay->ad_cname.bv_val, j, on->on_bi.bi_type );
6645                                 if ( rdn.bv_len >= sizeof( c.log ) ) {
6646                                         /* FIXME: holler ... */ ;
6647                                 }
6648                                 c.be = be;
6649                                 c.bi = &on->on_bi;
6650                                 oe = config_build_entry( op, &rs, ce, &c, &rdn,
6651                                         &CFOC_OVERLAY, c.bi->bi_cf_ocs );
6652                                 if ( !oe ) {
6653                                         return -1;
6654                                 }
6655                                 if ( c.bi->bi_cf_ocs && c.bi->bi_cf_ocs->co_cfadd )
6656                                         c.bi->bi_cf_ocs->co_cfadd( op, &rs, oe, &c );
6657                         }
6658                 }
6659         }
6660         if ( thrctx )
6661                 ldap_pvt_thread_pool_context_reset( thrctx );
6662
6663         if ( unsupp  && cfb->cb_use_ldif ) {
6664                 Debug( LDAP_DEBUG_ANY, "\nWARNING: The converted cn=config "
6665                         "directory is incomplete and may not work.\n\n", 0, 0, 0 );
6666         }
6667
6668         return 0;
6669 }
6670
6671 static void
6672 cfb_free_cffile( ConfigFile *cf )
6673 {
6674         ConfigFile *next;
6675
6676         for (; cf; cf=next) {
6677                 next = cf->c_sibs;
6678                 if ( cf->c_kids )
6679                         cfb_free_cffile( cf->c_kids );
6680                 ch_free( cf->c_file.bv_val );
6681                 ber_bvarray_free( cf->c_dseFiles );
6682                 ch_free( cf );
6683         }
6684 }
6685
6686 static void
6687 cfb_free_entries( CfEntryInfo *ce )
6688 {
6689         CfEntryInfo *next;
6690
6691         for (; ce; ce=next) {
6692                 next = ce->ce_sibs;
6693                 if ( ce->ce_kids )
6694                         cfb_free_entries( ce->ce_kids );
6695                 ce->ce_entry->e_private = NULL;
6696                 entry_free( ce->ce_entry );
6697                 ch_free( ce );
6698         }
6699 }
6700
6701 static int
6702 config_back_db_close( BackendDB *be, ConfigReply *cr )
6703 {
6704         CfBackInfo *cfb = be->be_private;
6705
6706         cfb_free_entries( cfb->cb_root );
6707         cfb->cb_root = NULL;
6708
6709         if ( cfb->cb_db.bd_info ) {
6710                 backend_shutdown( &cfb->cb_db );
6711         }
6712
6713         return 0;
6714 }
6715
6716 static int
6717 config_back_db_destroy( BackendDB *be, ConfigReply *cr )
6718 {
6719         CfBackInfo *cfb = be->be_private;
6720
6721         cfb_free_cffile( cfb->cb_config );
6722
6723         ch_free( cfdir.bv_val );
6724
6725         avl_free( CfOcTree, NULL );
6726
6727         if ( cfb->cb_db.bd_info ) {
6728                 cfb->cb_db.be_suffix = NULL;
6729                 cfb->cb_db.be_nsuffix = NULL;
6730                 BER_BVZERO( &cfb->cb_db.be_rootdn );
6731                 BER_BVZERO( &cfb->cb_db.be_rootndn );
6732
6733                 backend_destroy_one( &cfb->cb_db, 0 );
6734         }
6735
6736         loglevel_destroy();
6737
6738         return 0;
6739 }
6740
6741 static int
6742 config_back_db_init( BackendDB *be, ConfigReply* cr )
6743 {
6744         struct berval dn;
6745         CfBackInfo *cfb;
6746
6747         cfb = &cfBackInfo;
6748         cfb->cb_config = ch_calloc( 1, sizeof(ConfigFile));
6749         cfn = cfb->cb_config;
6750         be->be_private = cfb;
6751
6752         ber_dupbv( &be->be_rootdn, &config_rdn );
6753         ber_dupbv( &be->be_rootndn, &be->be_rootdn );
6754         ber_dupbv( &dn, &be->be_rootdn );
6755         ber_bvarray_add( &be->be_suffix, &dn );
6756         ber_dupbv( &dn, &be->be_rootdn );
6757         ber_bvarray_add( &be->be_nsuffix, &dn );
6758
6759         /* Hide from namingContexts */
6760         SLAP_BFLAGS(be) |= SLAP_BFLAG_CONFIG;
6761
6762         /* Check ACLs on content of Adds by default */
6763         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_ACL_ADD;
6764
6765         return 0;
6766 }
6767
6768 static int
6769 config_back_destroy( BackendInfo *bi )
6770 {
6771         ldif_must_b64_encode_release();
6772         return 0;
6773 }
6774
6775 static int
6776 config_tool_entry_open( BackendDB *be, int mode )
6777 {
6778         CfBackInfo *cfb = be->be_private;
6779         BackendInfo *bi = cfb->cb_db.bd_info;
6780
6781         if ( bi && bi->bi_tool_entry_open )
6782                 return bi->bi_tool_entry_open( &cfb->cb_db, mode );
6783         else
6784                 return -1;
6785         
6786 }
6787
6788 static int
6789 config_tool_entry_close( BackendDB *be )
6790 {
6791         CfBackInfo *cfb = be->be_private;
6792         BackendInfo *bi = cfb->cb_db.bd_info;
6793
6794         if ( bi && bi->bi_tool_entry_close )
6795                 return bi->bi_tool_entry_close( &cfb->cb_db );
6796         else
6797                 return -1;
6798 }
6799
6800 static ID
6801 config_tool_entry_first( BackendDB *be )
6802 {
6803         CfBackInfo *cfb = be->be_private;
6804         BackendInfo *bi = cfb->cb_db.bd_info;
6805
6806         if ( bi && bi->bi_tool_entry_first )
6807                 return bi->bi_tool_entry_first( &cfb->cb_db );
6808         else
6809                 return NOID;
6810 }
6811
6812 static ID
6813 config_tool_entry_next( BackendDB *be )
6814 {
6815         CfBackInfo *cfb = be->be_private;
6816         BackendInfo *bi = cfb->cb_db.bd_info;
6817
6818         if ( bi && bi->bi_tool_entry_next )
6819                 return bi->bi_tool_entry_next( &cfb->cb_db );
6820         else
6821                 return NOID;
6822 }
6823
6824 static Entry *
6825 config_tool_entry_get( BackendDB *be, ID id )
6826 {
6827         CfBackInfo *cfb = be->be_private;
6828         BackendInfo *bi = cfb->cb_db.bd_info;
6829
6830         if ( bi && bi->bi_tool_entry_get )
6831                 return bi->bi_tool_entry_get( &cfb->cb_db, id );
6832         else
6833                 return NULL;
6834 }
6835
6836 static int entry_put_got_frontend=0;
6837 static int entry_put_got_config=0;
6838 static ID
6839 config_tool_entry_put( BackendDB *be, Entry *e, struct berval *text )
6840 {
6841         CfBackInfo *cfb = be->be_private;
6842         BackendInfo *bi = cfb->cb_db.bd_info;
6843         int rc;
6844         struct berval rdn, vals[ 2 ];
6845         ConfigArgs ca;
6846         OperationBuffer opbuf;
6847         Entry *ce;
6848         Connection conn = {0};
6849         Operation *op = NULL;
6850         void *thrctx;
6851         int isFrontend = 0;
6852
6853         /* Create entry for frontend database if it does not exist already */
6854         if ( !entry_put_got_frontend ) {
6855                 if ( !strncmp( e->e_nname.bv_val, "olcDatabase", 
6856                                 STRLENOF( "olcDatabase" ))) {
6857                         if ( strncmp( e->e_nname.bv_val + 
6858                                         STRLENOF( "olcDatabase" ), "={-1}frontend",
6859                                         STRLENOF( "={-1}frontend" )) && 
6860                                         strncmp( e->e_nname.bv_val + 
6861                                         STRLENOF( "olcDatabase" ), "=frontend",
6862                                         STRLENOF( "=frontend" ))) {
6863                                 vals[1].bv_len = 0;
6864                                 vals[1].bv_val = NULL;
6865                                 memset( &ca, 0, sizeof(ConfigArgs));
6866                                 ca.be = frontendDB;
6867                                 ca.bi = frontendDB->bd_info;
6868                                 ca.be->be_cf_ocs = &CFOC_FRONTEND;
6869                                 rdn.bv_val = ca.log;
6870                                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( ca.log ),
6871                                         "%s=" SLAP_X_ORDERED_FMT "%s",
6872                                         cfAd_database->ad_cname.bv_val, -1,
6873                                         ca.bi->bi_type);
6874                                 ce = config_build_entry( NULL, NULL, cfb->cb_root, &ca, &rdn,
6875                                                 &CFOC_DATABASE, ca.be->be_cf_ocs );
6876                                 thrctx = ldap_pvt_thread_pool_context();
6877                                 connection_fake_init2( &conn, &opbuf, thrctx,0 );
6878                                 op = &opbuf.ob_op;
6879                                 op->o_bd = &cfb->cb_db;
6880                                 op->o_tag = LDAP_REQ_ADD;
6881                                 op->ora_e = ce;
6882                                 op->o_dn = be->be_rootdn;
6883                                 op->o_ndn = be->be_rootndn;
6884                                 rc = slap_add_opattrs(op, NULL, NULL, 0, 0);
6885                                 if ( rc != LDAP_SUCCESS ) {
6886                                         text->bv_val = "autocreation of \"olcDatabase={-1}frontend\" failed";
6887                                         text->bv_len = STRLENOF("autocreation of \"olcDatabase={-1}frontend\" failed");
6888                                         return NOID;
6889                                 }
6890
6891                                 if ( ce && bi && bi->bi_tool_entry_put && 
6892                                                 bi->bi_tool_entry_put( &cfb->cb_db, ce, text ) != NOID ) {
6893                                         entry_put_got_frontend++;
6894                                 } else {
6895                                         text->bv_val = "autocreation of \"olcDatabase={-1}frontend\" failed";
6896                                         text->bv_len = STRLENOF("autocreation of \"olcDatabase={-1}frontend\" failed");
6897                                         return NOID;
6898                                 }
6899                         } else {
6900                                 entry_put_got_frontend++;
6901                                 isFrontend = 1;
6902                         }
6903                 }
6904         }
6905         /* Create entry for config database if it does not exist already */
6906         if ( !entry_put_got_config && !isFrontend ) {
6907                 if ( !strncmp( e->e_nname.bv_val, "olcDatabase",
6908                                 STRLENOF( "olcDatabase" ))) {
6909                         if ( strncmp( e->e_nname.bv_val +
6910                                         STRLENOF( "olcDatabase" ), "={0}config",
6911                                         STRLENOF( "={0}config" )) &&
6912                                         strncmp( e->e_nname.bv_val +
6913                                         STRLENOF( "olcDatabase" ), "=config",
6914                                         STRLENOF( "=config" )) ) {
6915                                 vals[1].bv_len = 0;
6916                                 vals[1].bv_val = NULL;
6917                                 memset( &ca, 0, sizeof(ConfigArgs));
6918                                 ca.be = LDAP_STAILQ_FIRST( &backendDB );
6919                                 ca.bi = ca.be->bd_info;
6920                                 rdn.bv_val = ca.log;
6921                                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( ca.log ),
6922                                         "%s=" SLAP_X_ORDERED_FMT "%s",
6923                                         cfAd_database->ad_cname.bv_val, 0,
6924                                         ca.bi->bi_type);
6925                                 ce = config_build_entry( NULL, NULL, cfb->cb_root, &ca, &rdn, &CFOC_DATABASE,
6926                                                 ca.be->be_cf_ocs );
6927                                 if ( ! op ) {
6928                                         thrctx = ldap_pvt_thread_pool_context();
6929                                         connection_fake_init2( &conn, &opbuf, thrctx,0 );
6930                                         op = &opbuf.ob_op;
6931                                         op->o_bd = &cfb->cb_db;
6932                                         op->o_tag = LDAP_REQ_ADD;
6933                                         op->o_dn = be->be_rootdn;
6934                                         op->o_ndn = be->be_rootndn;
6935                                 }
6936                                 op->ora_e = ce;
6937                                 rc = slap_add_opattrs(op, NULL, NULL, 0, 0);
6938                                 if ( rc != LDAP_SUCCESS ) {
6939                                         text->bv_val = "autocreation of \"olcDatabase={0}config\" failed";
6940                                         text->bv_len = STRLENOF("autocreation of \"olcDatabase={0}config\" failed");
6941                                         return NOID;
6942                                 }
6943                                 if (ce && bi && bi->bi_tool_entry_put &&
6944                                                 bi->bi_tool_entry_put( &cfb->cb_db, ce, text ) != NOID ) {
6945                                         entry_put_got_config++;
6946                                 } else {
6947                                         text->bv_val = "autocreation of \"olcDatabase={0}config\" failed";
6948                                         text->bv_len = STRLENOF("autocreation of \"olcDatabase={0}config\" failed");
6949                                         return NOID;
6950                                 }
6951                         } else {
6952                                 entry_put_got_config++;
6953                         }
6954                 }
6955         }
6956         if ( bi && bi->bi_tool_entry_put &&
6957                 config_add_internal( cfb, e, &ca, NULL, NULL, NULL ) == 0 )
6958                 return bi->bi_tool_entry_put( &cfb->cb_db, e, text );
6959         else
6960                 return NOID;
6961 }
6962
6963 static struct {
6964         char *name;
6965         AttributeDescription **desc;
6966 } ads[] = {
6967         { "attribute", &cfAd_attr },
6968         { "backend", &cfAd_backend },
6969         { "database", &cfAd_database },
6970         { "include", &cfAd_include },
6971         { "ldapsyntax", &cfAd_syntax },
6972         { "objectclass", &cfAd_oc },
6973         { "objectidentifier", &cfAd_om },
6974         { "overlay", &cfAd_overlay },
6975         { NULL, NULL }
6976 };
6977
6978 /* Notes:
6979  *   add / delete: all types that may be added or deleted must use an
6980  * X-ORDERED attributeType for their RDN. Adding and deleting entries
6981  * should automatically renumber the index of any siblings as needed,
6982  * so that no gaps in the numbering sequence exist after the add/delete
6983  * is completed.
6984  *   What can be added:
6985  *     schema objects
6986  *     backend objects for backend-specific config directives
6987  *     database objects
6988  *     overlay objects
6989  *
6990  *   delete: probably no support this time around.
6991  *
6992  *   modrdn: generally not done. Will be invoked automatically by add/
6993  * delete to update numbering sequence. Perform as an explicit operation
6994  * so that the renumbering effect may be replicated. Subtree rename must
6995  * be supported, since renumbering a database will affect all its child
6996  * overlays.
6997  *
6998  *  modify: must be fully supported. 
6999  */
7000
7001 int
7002 config_back_initialize( BackendInfo *bi )
7003 {
7004         ConfigTable             *ct = config_back_cf_table;
7005         ConfigArgs ca;
7006         char                    *argv[4];
7007         int                     i;
7008         AttributeDescription    *ad = NULL;
7009         const char              *text;
7010         static char             *controls[] = {
7011                 LDAP_CONTROL_MANAGEDSAIT,
7012                 NULL
7013         };
7014
7015         /* Make sure we don't exceed the bits reserved for userland */
7016         config_check_userland( CFG_LAST );
7017
7018         bi->bi_controls = controls;
7019
7020         bi->bi_open = 0;
7021         bi->bi_close = 0;
7022         bi->bi_config = 0;
7023         bi->bi_destroy = config_back_destroy;
7024
7025         bi->bi_db_init = config_back_db_init;
7026         bi->bi_db_config = 0;
7027         bi->bi_db_open = config_back_db_open;
7028         bi->bi_db_close = config_back_db_close;
7029         bi->bi_db_destroy = config_back_db_destroy;
7030
7031         bi->bi_op_bind = config_back_bind;
7032         bi->bi_op_unbind = 0;
7033         bi->bi_op_search = config_back_search;
7034         bi->bi_op_compare = 0;
7035         bi->bi_op_modify = config_back_modify;
7036         bi->bi_op_modrdn = config_back_modrdn;
7037         bi->bi_op_add = config_back_add;
7038         bi->bi_op_delete = config_back_delete;
7039         bi->bi_op_abandon = 0;
7040
7041         bi->bi_extended = 0;
7042
7043         bi->bi_chk_referrals = 0;
7044
7045         bi->bi_access_allowed = slap_access_allowed;
7046
7047         bi->bi_connection_init = 0;
7048         bi->bi_connection_destroy = 0;
7049
7050         bi->bi_entry_release_rw = config_entry_release;
7051         bi->bi_entry_get_rw = config_back_entry_get;
7052
7053         bi->bi_tool_entry_open = config_tool_entry_open;
7054         bi->bi_tool_entry_close = config_tool_entry_close;
7055         bi->bi_tool_entry_first = config_tool_entry_first;
7056         bi->bi_tool_entry_next = config_tool_entry_next;
7057         bi->bi_tool_entry_get = config_tool_entry_get;
7058         bi->bi_tool_entry_put = config_tool_entry_put;
7059
7060         ca.argv = argv;
7061         argv[ 0 ] = "slapd";
7062         ca.argv = argv;
7063         ca.argc = 3;
7064         ca.fname = argv[0];
7065
7066         argv[3] = NULL;
7067         for (i=0; OidMacros[i].name; i++ ) {
7068                 argv[1] = OidMacros[i].name;
7069                 argv[2] = OidMacros[i].oid;
7070                 parse_oidm( &ca, 0, NULL );
7071         }
7072
7073         bi->bi_cf_ocs = cf_ocs;
7074
7075         i = config_register_schema( ct, cf_ocs );
7076         if ( i ) return i;
7077
7078         i = slap_str2ad( "olcDatabase", &olcDatabaseDummy[0].ad, &text );
7079         if ( i ) return i;
7080
7081         /* setup olcRootPW to be base64-encoded when written in LDIF form;
7082          * basically, we don't care if it fails */
7083         i = slap_str2ad( "olcRootPW", &ad, &text );
7084         if ( i ) {
7085                 Debug( LDAP_DEBUG_ANY, "config_back_initialize: "
7086                         "warning, unable to get \"olcRootPW\" "
7087                         "attribute description: %d: %s\n",
7088                         i, text, 0 );
7089         } else {
7090                 (void)ldif_must_b64_encode_register( ad->ad_cname.bv_val,
7091                         ad->ad_type->sat_oid );
7092         }
7093
7094         /* set up the notable AttributeDescriptions */
7095         i = 0;
7096         for (;ct->name;ct++) {
7097                 if (strcmp(ct->name, ads[i].name)) continue;
7098                 *ads[i].desc = ct->ad;
7099                 i++;
7100                 if (!ads[i].name) break;
7101         }
7102
7103         return 0;
7104 }