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