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