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