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