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