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