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