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