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