]> git.sur5r.net Git - openldap/blob - servers/slapd/bconfig.c
complete merging of dyngroup into dynlist; add back-config support
[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 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 static struct berval config_rdn = BER_BVC("cn=config");
41 static struct berval schema_rdn = BER_BVC("cn=schema");
42
43 #ifdef SLAPD_MODULES
44 typedef struct modpath_s {
45         struct modpath_s *mp_next;
46         struct berval mp_path;
47         BerVarray mp_loads;
48 } ModPaths;
49
50 static ModPaths modpaths, *modlast = &modpaths, *modcur = &modpaths;
51 #endif
52
53 typedef struct ConfigFile {
54         struct ConfigFile *c_sibs;
55         struct ConfigFile *c_kids;
56         struct berval c_file;
57         AttributeType *c_at_head, *c_at_tail;
58         ContentRule *c_cr_head, *c_cr_tail;
59         ObjectClass *c_oc_head, *c_oc_tail;
60         OidMacro *c_om_head, *c_om_tail;
61         BerVarray c_dseFiles;
62 } ConfigFile;
63
64 typedef struct {
65         ConfigFile *cb_config;
66         CfEntryInfo *cb_root;
67         BackendDB       cb_db;  /* underlying database */
68         int             cb_got_ldif;
69         int             cb_use_ldif;
70 } CfBackInfo;
71
72 /* These do nothing in slapd, they're kept only to make them
73  * editable here.
74  */
75 static char *replica_pidFile, *replica_argsFile;
76 static int replicationInterval;
77
78 static char     *passwd_salt;
79 static char     *logfileName;
80 #ifdef SLAP_AUTH_REWRITE
81 static BerVarray authz_rewrites;
82 #endif
83
84 static struct berval cfdir;
85
86 /* Private state */
87 static AttributeDescription *cfAd_backend, *cfAd_database, *cfAd_overlay,
88         *cfAd_include;
89
90 static ConfigFile *cfn;
91
92 static Avlnode *CfOcTree;
93
94 static int config_add_internal( CfBackInfo *cfb, Entry *e, ConfigArgs *ca,
95         SlapReply *rs, int *renumber );
96
97 static ConfigDriver config_fname;
98 static ConfigDriver config_cfdir;
99 static ConfigDriver config_generic;
100 static ConfigDriver config_search_base;
101 static ConfigDriver config_passwd_hash;
102 static ConfigDriver config_schema_dn;
103 static ConfigDriver config_sizelimit;
104 static ConfigDriver config_timelimit;
105 static ConfigDriver config_overlay;
106 static ConfigDriver config_subordinate; 
107 static ConfigDriver config_suffix; 
108 static ConfigDriver config_rootdn;
109 static ConfigDriver config_rootpw;
110 static ConfigDriver config_restrict;
111 static ConfigDriver config_allows;
112 static ConfigDriver config_disallows;
113 static ConfigDriver config_requires;
114 static ConfigDriver config_security;
115 static ConfigDriver config_referral;
116 static ConfigDriver config_loglevel;
117 static ConfigDriver config_replica;
118 static ConfigDriver config_updatedn;
119 static ConfigDriver config_updateref;
120 static ConfigDriver config_include;
121 #ifdef HAVE_TLS
122 static ConfigDriver config_tls_option;
123 static ConfigDriver config_tls_config;
124 #endif
125 extern ConfigDriver syncrepl_config;
126
127 enum {
128         CFG_ACL = 1,
129         CFG_BACKEND,
130         CFG_DATABASE,
131         CFG_TLS_RAND,
132         CFG_TLS_CIPHER,
133         CFG_TLS_CERT_FILE,
134         CFG_TLS_CERT_KEY,
135         CFG_TLS_CA_PATH,
136         CFG_TLS_CA_FILE,
137         CFG_TLS_DH_FILE,
138         CFG_TLS_VERIFY,
139         CFG_TLS_CRLCHECK,
140         CFG_CONCUR,
141         CFG_THREADS,
142         CFG_SALT,
143         CFG_LIMITS,
144         CFG_RO,
145         CFG_REWRITE,
146         CFG_DEPTH,
147         CFG_OID,
148         CFG_OC,
149         CFG_DIT,
150         CFG_ATTR,
151         CFG_ATOPT,
152         CFG_REPLOG,
153         CFG_ROOTDSE,
154         CFG_LOGFILE,
155         CFG_PLUGIN,
156         CFG_MODLOAD,
157         CFG_MODPATH,
158         CFG_LASTMOD,
159         CFG_AZPOLICY,
160         CFG_AZREGEXP,
161         CFG_SASLSECP,
162         CFG_SSTR_IF_MAX,
163         CFG_SSTR_IF_MIN,
164         CFG_TTHREADS,
165
166         CFG_LAST
167 };
168
169 typedef struct {
170         char *name, *oid;
171 } OidRec;
172
173 static OidRec OidMacros[] = {
174         /* OpenLDAProot:666.11.1 */
175         { "OLcfg", "1.3.6.1.4.1.4203.666.11.1" },
176         { "OLcfgAt", "OLcfg:3" },
177         { "OLcfgGlAt", "OLcfgAt:0" },
178         { "OLcfgBkAt", "OLcfgAt:1" },
179         { "OLcfgDbAt", "OLcfgAt:2" },
180         { "OLcfgOvAt", "OLcfgAt:3" },
181         { "OLcfgOc", "OLcfg:4" },
182         { "OLcfgGlOc", "OLcfgOc:0" },
183         { "OLcfgBkOc", "OLcfgOc:1" },
184         { "OLcfgDbOc", "OLcfgOc:2" },
185         { "OLcfgOvOc", "OLcfgOc:3" },
186         { "OMsyn", "1.3.6.1.4.1.1466.115.121.1" },
187         { "OMsBoolean", "OMsyn:7" },
188         { "OMsDN", "OMsyn:12" },
189         { "OMsDirectoryString", "OMsyn:15" },
190         { "OMsInteger", "OMsyn:27" },
191         { "OMsOID", "OMsyn:38" },
192         { "OMsOctetString", "OMsyn:40" },
193         { NULL, NULL }
194 };
195
196 /*
197  * Backend/Database registry
198  *
199  * OLcfg{Bk|Db}{Oc|At}:0                -> common
200  * OLcfg{Bk|Db}{Oc|At}:1                -> back-bdb(/back-hdb)
201  * OLcfg{Bk|Db}{Oc|At}:2                -> back-ldif
202  * OLcfg{Bk|Db}{Oc|At}:3                -> back-ldap
203  */
204
205 /*
206  * Overlay registry
207  *
208  * OLcfgOv{Oc|At}:1                     -> syncprov
209  * OLcfgOv{Oc|At}:2                     -> pcache
210  * OLcfgOv{Oc|At}:3                     -> chain
211  * OLcfgOv{Oc|At}:4                     -> accesslog
212  * OLcfgOv{Oc|At}:5                     -> valsort
213  * (FIXME: separate arc for contribware?)
214  * OLcfgOv{Oc|At}:6                     -> smbk5pwd
215  * OLcfgOv{Oc|At}:7                     -> distproc
216  * OLcfgOv{Oc|At}:8                     -> dynlist
217  */
218
219 /* alphabetical ordering */
220
221 static ConfigTable config_back_cf_table[] = {
222         /* This attr is read-only */
223         { "", "", 0, 0, 0, ARG_MAGIC,
224                 &config_fname, "( OLcfgGlAt:78 NAME 'olcConfigFile' "
225                         "DESC 'File for slapd configuration directives' "
226                         "EQUALITY caseIgnoreMatch "
227                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
228         { "", "", 0, 0, 0, ARG_MAGIC,
229                 &config_cfdir, "( OLcfgGlAt:79 NAME 'olcConfigDir' "
230                         "DESC 'Directory for slapd configuration backend' "
231                         "EQUALITY caseIgnoreMatch "
232                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
233         { "access",     NULL, 0, 0, 0, ARG_MAY_DB|ARG_MAGIC|CFG_ACL,
234                 &config_generic, "( OLcfgGlAt:1 NAME 'olcAccess' "
235                         "DESC 'Access Control List' "
236                         "EQUALITY caseIgnoreMatch "
237                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
238         { "allows",     "features", 2, 0, 5, ARG_PRE_DB|ARG_MAGIC,
239                 &config_allows, "( OLcfgGlAt:2 NAME 'olcAllows' "
240                         "DESC 'Allowed set of deprecated features' "
241                         "EQUALITY caseIgnoreMatch "
242                         "SYNTAX OMsDirectoryString )", NULL, NULL },
243         { "argsfile", "file", 2, 2, 0, ARG_STRING,
244                 &slapd_args_file, "( OLcfgGlAt:3 NAME 'olcArgsFile' "
245                         "DESC 'File for slapd command line options' "
246                         "EQUALITY caseIgnoreMatch "
247                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
248         { "attributeoptions", NULL, 0, 0, 0, ARG_MAGIC|CFG_ATOPT,
249                 &config_generic, "( OLcfgGlAt:5 NAME 'olcAttributeOptions' "
250                         "EQUALITY caseIgnoreMatch "
251                         "SYNTAX OMsDirectoryString )", NULL, NULL },
252         { "attribute",  "attribute", 2, 0, 9,
253                 ARG_PAREN|ARG_MAGIC|CFG_ATTR|ARG_NO_DELETE|ARG_NO_INSERT,
254                 &config_generic, "( OLcfgGlAt:4 NAME 'olcAttributeTypes' "
255                         "DESC 'OpenLDAP attributeTypes' "
256                         "EQUALITY caseIgnoreMatch "
257                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )",
258                                 NULL, NULL },
259         { "authid-rewrite", NULL, 2, 0, STRLENOF( "authid-rewrite" ),
260 #ifdef SLAP_AUTH_REWRITE
261                 ARG_MAGIC|CFG_REWRITE|ARG_NO_INSERT, &config_generic,
262 #else
263                 ARG_IGNORED, NULL,
264 #endif
265                  "( OLcfgGlAt:6 NAME 'olcAuthIDRewrite' "
266                         "EQUALITY caseIgnoreMatch "
267                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
268         { "authz-policy", "policy", 2, 2, 0, ARG_STRING|ARG_MAGIC|CFG_AZPOLICY,
269                 &config_generic, "( OLcfgGlAt:7 NAME 'olcAuthzPolicy' "
270                         "EQUALITY caseIgnoreMatch "
271                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
272         { "authz-regexp", NULL, 3, 3, 0, ARG_MAGIC|CFG_AZREGEXP|ARG_NO_INSERT,
273                 &config_generic, "( OLcfgGlAt:8 NAME 'olcAuthzRegexp' "
274                         "EQUALITY caseIgnoreMatch "
275                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
276         { "backend", "type", 2, 2, 0, ARG_PRE_DB|ARG_MAGIC|CFG_BACKEND,
277                 &config_generic, "( OLcfgGlAt:9 NAME 'olcBackend' "
278                         "DESC 'A type of backend' "
279                         "EQUALITY caseIgnoreMatch "
280                         "SYNTAX OMsDirectoryString SINGLE-VALUE X-ORDERED 'SIBLINGS' )",
281                                 NULL, NULL },
282         { "concurrency", "level", 2, 2, 0, ARG_INT|ARG_MAGIC|CFG_CONCUR,
283                 &config_generic, "( OLcfgGlAt:10 NAME 'olcConcurrency' "
284                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
285         { "conn_max_pending", "max", 2, 2, 0, ARG_INT,
286                 &slap_conn_max_pending, "( OLcfgGlAt:11 NAME 'olcConnMaxPending' "
287                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
288         { "conn_max_pending_auth", "max", 2, 2, 0, ARG_INT,
289                 &slap_conn_max_pending_auth, "( OLcfgGlAt:12 NAME 'olcConnMaxPendingAuth' "
290                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
291         { "database", "type", 2, 2, 0, ARG_MAGIC|CFG_DATABASE,
292                 &config_generic, "( OLcfgGlAt:13 NAME 'olcDatabase' "
293                         "DESC 'The backend type for a database instance' "
294                         "SUP olcBackend SINGLE-VALUE X-ORDERED 'SIBLINGS' )", NULL, NULL },
295         { "defaultSearchBase", "dn", 2, 2, 0, ARG_PRE_BI|ARG_PRE_DB|ARG_DN|ARG_QUOTE|ARG_MAGIC,
296                 &config_search_base, "( OLcfgGlAt:14 NAME 'olcDefaultSearchBase' "
297                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
298         { "disallows", "features", 2, 0, 8, ARG_PRE_DB|ARG_MAGIC,
299                 &config_disallows, "( OLcfgGlAt:15 NAME 'olcDisallows' "
300                         "EQUALITY caseIgnoreMatch "
301                         "SYNTAX OMsDirectoryString )", NULL, NULL },
302         { "ditcontentrule",     NULL, 0, 0, 0, ARG_MAGIC|CFG_DIT|ARG_NO_DELETE|ARG_NO_INSERT,
303                 &config_generic, "( OLcfgGlAt:16 NAME 'olcDitContentRules' "
304                         "DESC 'OpenLDAP DIT content rules' "
305                         "EQUALITY caseIgnoreMatch "
306                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )",
307                         NULL, NULL },
308         { "gentlehup", "on|off", 2, 2, 0,
309 #ifdef SIGHUP
310                 ARG_ON_OFF, &global_gentlehup,
311 #else
312                 ARG_IGNORED, NULL,
313 #endif
314                 "( OLcfgGlAt:17 NAME 'olcGentleHUP' "
315                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
316         { "idletimeout", "timeout", 2, 2, 0, ARG_INT,
317                 &global_idletimeout, "( OLcfgGlAt:18 NAME 'olcIdleTimeout' "
318                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
319         { "include", "file", 2, 2, 0, ARG_MAGIC,
320                 &config_include, "( OLcfgGlAt:19 NAME 'olcInclude' "
321                         "SUP labeledURI )", NULL, NULL },
322         { "index_substr_if_minlen", "min", 2, 2, 0, ARG_INT|ARG_NONZERO|ARG_MAGIC|CFG_SSTR_IF_MIN,
323                 &config_generic, "( OLcfgGlAt:20 NAME 'olcIndexSubstrIfMinLen' "
324                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
325         { "index_substr_if_maxlen", "max", 2, 2, 0, ARG_INT|ARG_NONZERO|ARG_MAGIC|CFG_SSTR_IF_MAX,
326                 &config_generic, "( OLcfgGlAt:21 NAME 'olcIndexSubstrIfMaxLen' "
327                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
328         { "index_substr_any_len", "len", 2, 2, 0, ARG_INT|ARG_NONZERO,
329                 &index_substr_any_len, "( OLcfgGlAt:22 NAME 'olcIndexSubstrAnyLen' "
330                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
331         { "index_substr_any_step", "step", 2, 2, 0, ARG_INT|ARG_NONZERO,
332                 &index_substr_any_step, "( OLcfgGlAt:23 NAME 'olcIndexSubstrAnyStep' "
333                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
334         { "lastmod", "on|off", 2, 2, 0, ARG_DB|ARG_ON_OFF|ARG_MAGIC|CFG_LASTMOD,
335                 &config_generic, "( OLcfgDbAt:0.4 NAME 'olcLastMod' "
336                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
337         { "limits", "limits", 2, 0, 0, ARG_DB|ARG_MAGIC|CFG_LIMITS,
338                 &config_generic, "( OLcfgDbAt:0.5 NAME 'olcLimits' "
339                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
340         { "localSSF", "ssf", 2, 2, 0, ARG_INT,
341                 &local_ssf, "( OLcfgGlAt:26 NAME 'olcLocalSSF' "
342                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
343         { "logfile", "file", 2, 2, 0, ARG_STRING|ARG_MAGIC|CFG_LOGFILE,
344                 &config_generic, "( OLcfgGlAt:27 NAME 'olcLogFile' "
345                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
346         { "loglevel", "level", 2, 0, 0, ARG_MAGIC,
347                 &config_loglevel, "( OLcfgGlAt:28 NAME 'olcLogLevel' "
348                         "SYNTAX OMsDirectoryString )", NULL, NULL },
349         { "maxDerefDepth", "depth", 2, 2, 0, ARG_DB|ARG_INT|ARG_MAGIC|CFG_DEPTH,
350                 &config_generic, "( OLcfgDbAt:0.6 NAME 'olcMaxDerefDepth' "
351                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
352         { "moduleload", "file", 2, 0, 0,
353 #ifdef SLAPD_MODULES
354                 ARG_MAGIC|CFG_MODLOAD, &config_generic,
355 #else
356                 ARG_IGNORED, NULL,
357 #endif
358                 "( OLcfgGlAt:30 NAME 'olcModuleLoad' "
359                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
360         { "modulepath", "path", 2, 2, 0,
361 #ifdef SLAPD_MODULES
362                 ARG_MAGIC|CFG_MODPATH|ARG_NO_DELETE|ARG_NO_INSERT, &config_generic,
363 #else
364                 ARG_IGNORED, NULL,
365 #endif
366                 "( OLcfgGlAt:31 NAME 'olcModulePath' "
367                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
368         { "objectclass", "objectclass", 2, 0, 0, ARG_PAREN|ARG_MAGIC|CFG_OC|ARG_NO_DELETE|ARG_NO_INSERT,
369                 &config_generic, "( OLcfgGlAt:32 NAME 'olcObjectClasses' "
370                 "DESC 'OpenLDAP object classes' "
371                 "EQUALITY caseIgnoreMatch "
372                 "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )",
373                         NULL, NULL },
374         { "objectidentifier", NULL,     0, 0, 0, ARG_MAGIC|CFG_OID,
375                 &config_generic, "( OLcfgGlAt:33 NAME 'olcObjectIdentifier' "
376                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
377         { "overlay", "overlay", 2, 2, 0, ARG_MAGIC,
378                 &config_overlay, "( OLcfgGlAt:34 NAME 'olcOverlay' "
379                         "SUP olcDatabase SINGLE-VALUE X-ORDERED 'SIBLINGS' )", NULL, NULL },
380         { "password-crypt-salt-format", "salt", 2, 2, 0, ARG_STRING|ARG_MAGIC|CFG_SALT,
381                 &config_generic, "( OLcfgGlAt:35 NAME 'olcPasswordCryptSaltFormat' "
382                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
383         { "password-hash", "hash", 2, 2, 0, ARG_MAGIC,
384                 &config_passwd_hash, "( OLcfgGlAt:36 NAME 'olcPasswordHash' "
385                         "SYNTAX OMsDirectoryString )", NULL, NULL },
386         { "pidfile", "file", 2, 2, 0, ARG_STRING,
387                 &slapd_pid_file, "( OLcfgGlAt:37 NAME 'olcPidFile' "
388                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
389         { "plugin", NULL, 0, 0, 0,
390 #ifdef LDAP_SLAPI
391                 ARG_MAGIC|CFG_PLUGIN, &config_generic,
392 #else
393                 ARG_IGNORED, NULL,
394 #endif
395                 "( OLcfgGlAt:38 NAME 'olcPlugin' "
396                         "SYNTAX OMsDirectoryString )", NULL, NULL },
397         { "pluginlog", "filename", 2, 2, 0,
398 #ifdef LDAP_SLAPI
399                 ARG_STRING, &slapi_log_file,
400 #else
401                 ARG_IGNORED, NULL,
402 #endif
403                 "( OLcfgGlAt:39 NAME 'olcPluginLogFile' "
404                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
405         { "readonly", "on|off", 2, 2, 0, ARG_MAY_DB|ARG_ON_OFF|ARG_MAGIC|CFG_RO,
406                 &config_generic, "( OLcfgGlAt:40 NAME 'olcReadOnly' "
407                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
408         { "referral", "url", 2, 2, 0, ARG_MAGIC,
409                 &config_referral, "( OLcfgGlAt:41 NAME 'olcReferral' "
410                         "SUP labeledURI SINGLE-VALUE )", NULL, NULL },
411         { "replica", "host or uri", 2, 0, 0, ARG_DB|ARG_MAGIC,
412                 &config_replica, "( OLcfgDbAt:0.7 NAME 'olcReplica' "
413                         "SUP labeledURI X-ORDERED 'VALUES' )", NULL, NULL },
414         { "replica-argsfile", NULL, 0, 0, 0, ARG_STRING,
415                 &replica_argsFile, "( OLcfgGlAt:43 NAME 'olcReplicaArgsFile' "
416                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
417         { "replica-pidfile", NULL, 0, 0, 0, ARG_STRING,
418                 &replica_pidFile, "( OLcfgGlAt:44 NAME 'olcReplicaPidFile' "
419                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
420         { "replicationInterval", NULL, 0, 0, 0, ARG_INT,
421                 &replicationInterval, "( OLcfgGlAt:45 NAME 'olcReplicationInterval' "
422                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
423         { "replogfile", "filename", 2, 2, 0, ARG_MAY_DB|ARG_MAGIC|ARG_STRING|CFG_REPLOG,
424                 &config_generic, "( OLcfgGlAt:46 NAME 'olcReplogFile' "
425                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
426         { "require", "features", 2, 0, 7, ARG_MAY_DB|ARG_MAGIC,
427                 &config_requires, "( OLcfgGlAt:47 NAME 'olcRequires' "
428                         "SYNTAX OMsDirectoryString )", NULL, NULL },
429         { "restrict", "op_list", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
430                 &config_restrict, "( OLcfgGlAt:48 NAME 'olcRestrict' "
431                         "SYNTAX OMsDirectoryString )", NULL, NULL },
432         { "reverse-lookup", "on|off", 2, 2, 0,
433 #ifdef SLAPD_RLOOKUPS
434                 ARG_ON_OFF, &use_reverse_lookup,
435 #else
436                 ARG_IGNORED, NULL,
437 #endif
438                 "( OLcfgGlAt:49 NAME 'olcReverseLookup' "
439                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
440         { "rootdn", "dn", 2, 2, 0, ARG_DB|ARG_DN|ARG_QUOTE|ARG_MAGIC,
441                 &config_rootdn, "( OLcfgDbAt:0.8 NAME 'olcRootDN' "
442                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
443         { "rootDSE", "file", 2, 2, 0, ARG_MAGIC|CFG_ROOTDSE,
444                 &config_generic, "( OLcfgGlAt:51 NAME 'olcRootDSE' "
445                         "SYNTAX OMsDirectoryString )", NULL, NULL },
446         { "rootpw", "password", 2, 2, 0, ARG_BERVAL|ARG_DB|ARG_MAGIC,
447                 &config_rootpw, "( OLcfgDbAt:0.9 NAME 'olcRootPW' "
448                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
449         { "sasl-authz-policy", NULL, 2, 2, 0, ARG_MAGIC|CFG_AZPOLICY,
450                 &config_generic, NULL, NULL, NULL },
451         { "sasl-host", "host", 2, 2, 0,
452 #ifdef HAVE_CYRUS_SASL
453                 ARG_STRING|ARG_UNIQUE, &global_host,
454 #else
455                 ARG_IGNORED, NULL,
456 #endif
457                 "( OLcfgGlAt:53 NAME 'olcSaslHost' "
458                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
459         { "sasl-realm", "realm", 2, 2, 0,
460 #ifdef HAVE_CYRUS_SASL
461                 ARG_STRING|ARG_UNIQUE, &global_realm,
462 #else
463                 ARG_IGNORED, NULL,
464 #endif
465                 "( OLcfgGlAt:54 NAME 'olcSaslRealm' "
466                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
467         { "sasl-regexp", NULL, 3, 3, 0, ARG_MAGIC|CFG_AZREGEXP,
468                 &config_generic, NULL, NULL, NULL },
469         { "sasl-secprops", "properties", 2, 2, 0,
470 #ifdef HAVE_CYRUS_SASL
471                 ARG_MAGIC|CFG_SASLSECP, &config_generic,
472 #else
473                 ARG_IGNORED, NULL,
474 #endif
475                 "( OLcfgGlAt:56 NAME 'olcSaslSecProps' "
476                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
477         { "saslRegexp", NULL, 3, 3, 0, ARG_MAGIC|CFG_AZREGEXP,
478                 &config_generic, NULL, NULL, NULL },
479         { "schemadn", "dn", 2, 2, 0, ARG_MAY_DB|ARG_DN|ARG_QUOTE|ARG_MAGIC,
480                 &config_schema_dn, "( OLcfgGlAt:58 NAME 'olcSchemaDN' "
481                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
482         { "security", "factors", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
483                 &config_security, "( OLcfgGlAt:59 NAME 'olcSecurity' "
484                         "SYNTAX OMsDirectoryString )", NULL, NULL },
485         { "sizelimit", "limit", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
486                 &config_sizelimit, "( OLcfgGlAt:60 NAME 'olcSizeLimit' "
487                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
488         { "sockbuf_max_incoming", "max", 2, 2, 0, ARG_BER_LEN_T,
489                 &sockbuf_max_incoming, "( OLcfgGlAt:61 NAME 'olcSockbufMaxIncoming' "
490                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
491         { "sockbuf_max_incoming_auth", "max", 2, 2, 0, ARG_BER_LEN_T,
492                 &sockbuf_max_incoming_auth, "( OLcfgGlAt:62 NAME 'olcSockbufMaxIncomingAuth' "
493                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
494         { "srvtab", "file", 2, 2, 0,
495 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
496                 ARG_STRING, &ldap_srvtab,
497 #else
498                 ARG_IGNORED, NULL,
499 #endif
500                 "( OLcfgGlAt:63 NAME 'olcSrvtab' "
501                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
502         { "subordinate", "[advertise]", 1, 2, 0, ARG_DB|ARG_MAGIC,
503                 &config_subordinate, "( OLcfgDbAt:0.15 NAME 'olcSubordinate' "
504                         "SYNTAX OMsDirectoryString )", NULL, NULL },
505         { "suffix",     "suffix", 2, 2, 0, ARG_DB|ARG_DN|ARG_QUOTE|ARG_MAGIC,
506                 &config_suffix, "( OLcfgDbAt:0.10 NAME 'olcSuffix' "
507                         "SYNTAX OMsDN )", NULL, NULL },
508         { "syncrepl", NULL, 0, 0, 0, ARG_DB|ARG_MAGIC,
509                 &syncrepl_config, "( OLcfgDbAt:0.11 NAME 'olcSyncrepl' "
510                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
511         { "threads", "count", 2, 2, 0, ARG_INT|ARG_MAGIC|CFG_THREADS,
512                 &config_generic, "( OLcfgGlAt:66 NAME 'olcThreads' "
513                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
514         { "timelimit", "limit", 2, 0, 0, ARG_MAY_DB|ARG_MAGIC,
515                 &config_timelimit, "( OLcfgGlAt:67 NAME 'olcTimeLimit' "
516                         "SYNTAX OMsDirectoryString )", NULL, NULL },
517         { "TLSCACertificateFile", NULL, 0, 0, 0,
518 #ifdef HAVE_TLS
519                 CFG_TLS_CA_FILE|ARG_STRING|ARG_MAGIC, &config_tls_option,
520 #else
521                 ARG_IGNORED, NULL,
522 #endif
523                 "( OLcfgGlAt:68 NAME 'olcTLSCACertificateFile' "
524                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
525         { "TLSCACertificatePath", NULL, 0, 0, 0,
526 #ifdef HAVE_TLS
527                 CFG_TLS_CA_PATH|ARG_STRING|ARG_MAGIC, &config_tls_option,
528 #else
529                 ARG_IGNORED, NULL,
530 #endif
531                 "( OLcfgGlAt:69 NAME 'olcTLSCACertificatePath' "
532                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
533         { "TLSCertificateFile", NULL, 0, 0, 0,
534 #ifdef HAVE_TLS
535                 CFG_TLS_CERT_FILE|ARG_STRING|ARG_MAGIC, &config_tls_option,
536 #else
537                 ARG_IGNORED, NULL,
538 #endif
539                 "( OLcfgGlAt:70 NAME 'olcTLSCertificateFile' "
540                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
541         { "TLSCertificateKeyFile", NULL, 0, 0, 0,
542 #ifdef HAVE_TLS
543                 CFG_TLS_CERT_KEY|ARG_STRING|ARG_MAGIC, &config_tls_option,
544 #else
545                 ARG_IGNORED, NULL,
546 #endif
547                 "( OLcfgGlAt:71 NAME 'olcTLSCertificateKeyFile' "
548                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
549         { "TLSCipherSuite",     NULL, 0, 0, 0,
550 #ifdef HAVE_TLS
551                 CFG_TLS_CIPHER|ARG_STRING|ARG_MAGIC, &config_tls_option,
552 #else
553                 ARG_IGNORED, NULL,
554 #endif
555                 "( OLcfgGlAt:72 NAME 'olcTLSCipherSuite' "
556                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
557         { "TLSCRLCheck", NULL, 0, 0, 0,
558 #if defined(HAVE_TLS) && defined(HAVE_OPENSSL_CRL)
559                 CFG_TLS_CRLCHECK|ARG_STRING|ARG_MAGIC, &config_tls_config,
560 #else
561                 ARG_IGNORED, NULL,
562 #endif
563                 "( OLcfgGlAt:73 NAME 'olcTLSCRLCheck' "
564                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
565         { "TLSRandFile", NULL, 0, 0, 0,
566 #ifdef HAVE_TLS
567                 CFG_TLS_RAND|ARG_STRING|ARG_MAGIC, &config_tls_option,
568 #else
569                 ARG_IGNORED, NULL,
570 #endif
571                 "( OLcfgGlAt:74 NAME 'olcTLSRandFile' "
572                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
573         { "TLSVerifyClient", NULL, 0, 0, 0,
574 #ifdef HAVE_TLS
575                 CFG_TLS_VERIFY|ARG_STRING|ARG_MAGIC, &config_tls_config,
576 #else
577                 ARG_IGNORED, NULL,
578 #endif
579                 "( OLcfgGlAt:75 NAME 'olcTLSVerifyClient' "
580                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
581         { "TLSDHParamFile", NULL, 0, 0, 0,
582 #ifdef HAVE_TLS
583                 CFG_TLS_DH_FILE|ARG_STRING|ARG_MAGIC, &config_tls_option,
584 #else
585                 ARG_IGNORED, NULL,
586 #endif
587                 "( OLcfgGlAt:77 NAME 'olcTLSDHParamFile' "
588                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
589         { "tool-threads", "count", 2, 2, 0, ARG_INT|ARG_MAGIC|CFG_TTHREADS,
590                 &config_generic, "( OLcfgGlAt:80 NAME 'olcToolThreads' "
591                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
592         { "ucdata-path", "path", 2, 2, 0, ARG_IGNORED,
593                 NULL, NULL, NULL, NULL },
594         { "updatedn", "dn", 2, 2, 0, ARG_DB|ARG_DN|ARG_QUOTE|ARG_MAGIC,
595                 &config_updatedn, "( OLcfgDbAt:0.12 NAME 'olcUpdateDN' "
596                         "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
597         { "updateref", "url", 2, 2, 0, ARG_DB|ARG_MAGIC,
598                 &config_updateref, "( OLcfgDbAt:0.13 NAME 'olcUpdateRef' "
599                         "SUP labeledURI )", NULL, NULL },
600         { NULL, NULL, 0, 0, 0, ARG_IGNORED,
601                 NULL, NULL, NULL, NULL }
602 };
603
604 /* Routines to check if a child can be added to this type */
605 static ConfigLDAPadd cfAddSchema, cfAddInclude, cfAddDatabase,
606         cfAddBackend, cfAddModule, cfAddOverlay;
607
608 /* NOTE: be careful when defining array members
609  * that can be conditionally compiled */
610 #define CFOC_GLOBAL     cf_ocs[1]
611 #define CFOC_SCHEMA     cf_ocs[2]
612 #define CFOC_BACKEND    cf_ocs[3]
613 #define CFOC_DATABASE   cf_ocs[4]
614 #define CFOC_OVERLAY    cf_ocs[5]
615 #define CFOC_INCLUDE    cf_ocs[6]
616 #define CFOC_FRONTEND   cf_ocs[7]
617 #ifdef SLAPD_MODULES
618 #define CFOC_MODULE     cf_ocs[8]
619 #endif /* SLAPD_MODULES */
620
621 static ConfigOCs cf_ocs[] = {
622         { "( OLcfgGlOc:0 "
623                 "NAME 'olcConfig' "
624                 "DESC 'OpenLDAP configuration object' "
625                 "ABSTRACT SUP top )", Cft_Abstract, NULL },
626         { "( OLcfgGlOc:1 "
627                 "NAME 'olcGlobal' "
628                 "DESC 'OpenLDAP Global configuration options' "
629                 "SUP olcConfig STRUCTURAL "
630                 "MAY ( cn $ olcConfigFile $ olcConfigDir $ olcAllows $ olcArgsFile $ "
631                  "olcAttributeOptions $ olcAuthIDRewrite $ "
632                  "olcAuthzPolicy $ olcAuthzRegexp $ olcConcurrency $ "
633                  "olcConnMaxPending $ olcConnMaxPendingAuth $ "
634                  "olcDisallows $ olcGentleHUP $ olcIdleTimeout $ "
635                  "olcIndexSubstrIfMaxLen $ olcIndexSubstrIfMinLen $ "
636                  "olcIndexSubstrAnyLen $ olcIndexSubstrAnyStep $ olcLocalSSF $ "
637                  "olcLogLevel $ "
638                  "olcPasswordCryptSaltFormat $ olcPasswordHash $ olcPidFile $ "
639                  "olcPluginLogFile $ olcReadOnly $ olcReferral $ "
640                  "olcReplicaPidFile $ olcReplicaArgsFile $ olcReplicationInterval $ "
641                  "olcReplogFile $ olcRequires $ olcRestrict $ olcReverseLookup $ "
642                  "olcRootDSE $ "
643                  "olcSaslHost $ olcSaslRealm $ olcSaslSecProps $ "
644                  "olcSecurity $ olcSizeLimit $ "
645                  "olcSockbufMaxIncoming $ olcSockbufMaxIncomingAuth $ olcSrvtab $ "
646                  "olcThreads $ olcTimeLimit $ olcTLSCACertificateFile $ "
647                  "olcTLSCACertificatePath $ olcTLSCertificateFile $ "
648                  "olcTLSCertificateKeyFile $ olcTLSCipherSuite $ olcTLSCRLCheck $ "
649                  "olcTLSRandFile $ olcTLSVerifyClient $ olcTLSDHParamFile $ "
650                  "olcToolThreads $ "
651                  "olcObjectIdentifier $ olcAttributeTypes $ olcObjectClasses $ "
652                  "olcDitContentRules ) )", Cft_Global },
653         { "( OLcfgGlOc:2 "
654                 "NAME 'olcSchemaConfig' "
655                 "DESC 'OpenLDAP schema object' "
656                 "SUP olcConfig STRUCTURAL "
657                 "MAY ( cn $ olcObjectIdentifier $ olcAttributeTypes $ "
658                  "olcObjectClasses $ olcDitContentRules ) )",
659                         Cft_Schema, NULL, cfAddSchema },
660         { "( OLcfgGlOc:3 "
661                 "NAME 'olcBackendConfig' "
662                 "DESC 'OpenLDAP Backend-specific options' "
663                 "SUP olcConfig STRUCTURAL "
664                 "MUST olcBackend )", Cft_Backend, NULL, cfAddBackend },
665         { "( OLcfgGlOc:4 "
666                 "NAME 'olcDatabaseConfig' "
667                 "DESC 'OpenLDAP Database-specific options' "
668                 "SUP olcConfig STRUCTURAL "
669                 "MUST olcDatabase "
670                 "MAY ( olcSuffix $ olcSubordinate $ olcAccess $ olcLastMod $ olcLimits $ "
671                  "olcMaxDerefDepth $ olcPlugin $ olcReadOnly $ olcReplica $ "
672                  "olcReplogFile $ olcRequires $ olcRestrict $ olcRootDN $ olcRootPW $ "
673                  "olcSchemaDN $ olcSecurity $ olcSizeLimit $ olcSyncrepl $ "
674                  "olcTimeLimit $ olcUpdateDN $ olcUpdateRef ) )",
675                         Cft_Database, NULL, cfAddDatabase },
676         { "( OLcfgGlOc:5 "
677                 "NAME 'olcOverlayConfig' "
678                 "DESC 'OpenLDAP Overlay-specific options' "
679                 "SUP olcConfig STRUCTURAL "
680                 "MUST olcOverlay )", Cft_Overlay, NULL, cfAddOverlay },
681         { "( OLcfgGlOc:6 "
682                 "NAME 'olcIncludeFile' "
683                 "DESC 'OpenLDAP configuration include file' "
684                 "SUP olcConfig STRUCTURAL "
685                 "MUST olcInclude "
686                 "MAY ( cn $ olcRootDSE ) )",
687                 Cft_Include, NULL, cfAddInclude },
688         /* This should be STRUCTURAL like all the other database classes, but
689          * that would mean inheriting all of the olcDatabaseConfig attributes,
690          * which causes them to be merged twice in config_build_entry.
691          */
692         { "( OLcfgGlOc:7 "
693                 "NAME 'olcFrontendConfig' "
694                 "DESC 'OpenLDAP frontend configuration' "
695                 "AUXILIARY "
696                 "MAY olcDefaultSearchBase )",
697                 Cft_Database, NULL, NULL },
698 #ifdef SLAPD_MODULES
699         { "( OLcfgGlOc:8 "
700                 "NAME 'olcModuleList' "
701                 "DESC 'OpenLDAP dynamic module info' "
702                 "SUP olcConfig STRUCTURAL "
703                 "MAY ( cn $ olcModulePath $ olcModuleLoad ) )",
704                 Cft_Module, NULL, cfAddModule },
705 #endif
706         { NULL, 0, NULL }
707 };
708
709 static int
710 config_generic(ConfigArgs *c) {
711         char *p;
712         int i;
713
714         if ( c->op == SLAP_CONFIG_EMIT ) {
715                 int rc = 0;
716                 switch(c->type) {
717                 case CFG_CONCUR:
718                         c->value_int = ldap_pvt_thread_get_concurrency();
719                         break;
720                 case CFG_THREADS:
721                         c->value_int = connection_pool_max;
722                         break;
723                 case CFG_TTHREADS:
724                         c->value_int = slap_tool_thread_max;
725                         break;
726                 case CFG_SALT:
727                         if ( passwd_salt )
728                                 c->value_string = ch_strdup( passwd_salt );
729                         else
730                                 rc = 1;
731                         break;
732                 case CFG_LIMITS:
733                         if ( c->be->be_limits ) {
734                                 char buf[4096*3];
735                                 struct berval bv;
736                                 int i;
737
738                                 for ( i=0; c->be->be_limits[i]; i++ ) {
739                                         bv.bv_len = snprintf( buf, sizeof( buf ), SLAP_X_ORDERED_FMT, i );
740                                         if ( bv.bv_len >= sizeof( buf ) ) {
741                                                 ber_bvarray_free_x( c->rvalue_vals, NULL );
742                                                 c->rvalue_vals = NULL;
743                                                 rc = 1;
744                                                 break;
745                                         }
746                                         bv.bv_val = buf + bv.bv_len;
747                                         limits_unparse( c->be->be_limits[i], &bv );
748                                         bv.bv_len += bv.bv_val - buf;
749                                         bv.bv_val = buf;
750                                         value_add_one( &c->rvalue_vals, &bv );
751                                 }
752                         }
753                         if ( !c->rvalue_vals ) rc = 1;
754                         break;
755                 case CFG_RO:
756                         c->value_int = (c->be->be_restrictops & SLAP_RESTRICT_OP_WRITES) ==
757                                 SLAP_RESTRICT_OP_WRITES;
758                         break;
759                 case CFG_AZPOLICY:
760                         c->value_string = ch_strdup( slap_sasl_getpolicy());
761                         break;
762                 case CFG_AZREGEXP:
763                         slap_sasl_regexp_unparse( &c->rvalue_vals );
764                         if ( !c->rvalue_vals ) rc = 1;
765                         break;
766 #ifdef HAVE_CYRUS_SASL
767                 case CFG_SASLSECP: {
768                         struct berval bv = BER_BVNULL;
769                         slap_sasl_secprops_unparse( &bv );
770                         if ( !BER_BVISNULL( &bv )) {
771                                 ber_bvarray_add( &c->rvalue_vals, &bv );
772                         } else {
773                                 rc = 1;
774                         }
775                         }
776                         break;
777 #endif
778                 case CFG_DEPTH:
779                         c->value_int = c->be->be_max_deref_depth;
780                         break;
781                 case CFG_OID: {
782                         ConfigFile *cf = c->private;
783                         if ( !cf )
784                                 oidm_unparse( &c->rvalue_vals, NULL, NULL, 1 );
785                         else if ( cf->c_om_head )
786                                 oidm_unparse( &c->rvalue_vals, cf->c_om_head,
787                                         cf->c_om_tail, 0 );
788                         if ( !c->rvalue_vals )
789                                 rc = 1;
790                         }
791                         break;
792                 case CFG_OC: {
793                         ConfigFile *cf = c->private;
794                         if ( !cf )
795                                 oc_unparse( &c->rvalue_vals, NULL, NULL, 1 );
796                         else if ( cf->c_oc_head )
797                                 oc_unparse( &c->rvalue_vals, cf->c_oc_head,
798                                         cf->c_oc_tail, 0 );
799                         if ( !c->rvalue_vals )
800                                 rc = 1;
801                         }
802                         break;
803                 case CFG_ATTR: {
804                         ConfigFile *cf = c->private;
805                         if ( !cf )
806                                 at_unparse( &c->rvalue_vals, NULL, NULL, 1 );
807                         else if ( cf->c_at_head )
808                                 at_unparse( &c->rvalue_vals, cf->c_at_head,
809                                         cf->c_at_tail, 0 );
810                         if ( !c->rvalue_vals )
811                                 rc = 1;
812                         }
813                         break;
814                 case CFG_DIT: {
815                         ConfigFile *cf = c->private;
816                         if ( !cf )
817                                 cr_unparse( &c->rvalue_vals, NULL, NULL, 1 );
818                         else if ( cf->c_cr_head )
819                                 cr_unparse( &c->rvalue_vals, cf->c_cr_head,
820                                         cf->c_cr_tail, 0 );
821                         if ( !c->rvalue_vals )
822                                 rc = 1;
823                         }
824                         break;
825                         
826                 case CFG_ACL: {
827                         AccessControl *a;
828                         char *src, *dst, ibuf[11];
829                         struct berval bv, abv;
830                         for (i=0, a=c->be->be_acl; a; i++,a=a->acl_next) {
831                                 abv.bv_len = snprintf( ibuf, sizeof( ibuf ), SLAP_X_ORDERED_FMT, i );
832                                 if ( abv.bv_len >= sizeof( ibuf ) ) {
833                                         ber_bvarray_free_x( c->rvalue_vals, NULL );
834                                         c->rvalue_vals = NULL;
835                                         i = 0;
836                                         break;
837                                 }
838                                 acl_unparse( a, &bv );
839                                 abv.bv_val = ch_malloc( abv.bv_len + bv.bv_len + 1 );
840                                 AC_MEMCPY( abv.bv_val, ibuf, abv.bv_len );
841                                 /* Turn TAB / EOL into plain space */
842                                 for (src=bv.bv_val,dst=abv.bv_val+abv.bv_len; *src; src++) {
843                                         if (isspace(*src)) *dst++ = ' ';
844                                         else *dst++ = *src;
845                                 }
846                                 *dst = '\0';
847                                 if (dst[-1] == ' ') {
848                                         dst--;
849                                         *dst = '\0';
850                                 }
851                                 abv.bv_len = dst - abv.bv_val;
852                                 ber_bvarray_add( &c->rvalue_vals, &abv );
853                         }
854                         rc = (!i);
855                         break;
856                 }
857                 case CFG_REPLOG:
858                         if ( c->be->be_replogfile )
859                                 c->value_string = ch_strdup( c->be->be_replogfile );
860                         break;
861                 case CFG_ROOTDSE: {
862                         ConfigFile *cf = c->private;
863                         if ( cf->c_dseFiles ) {
864                                 value_add( &c->rvalue_vals, cf->c_dseFiles );
865                         } else {
866                                 rc = 1;
867                         }
868                         }
869                         break;
870                 case CFG_LOGFILE:
871                         if ( logfileName )
872                                 c->value_string = ch_strdup( logfileName );
873                         else
874                                 rc = 1;
875                         break;
876                 case CFG_LASTMOD:
877                         c->value_int = (SLAP_NOLASTMOD(c->be) == 0);
878                         break;
879                 case CFG_SSTR_IF_MAX:
880                         c->value_int = index_substr_if_maxlen;
881                         break;
882                 case CFG_SSTR_IF_MIN:
883                         c->value_int = index_substr_if_minlen;
884                         break;
885 #ifdef SLAPD_MODULES
886                 case CFG_MODLOAD: {
887                         ModPaths *mp = c->private;
888                         if (mp->mp_loads) {
889                                 int i;
890                                 for (i=0; !BER_BVISNULL(&mp->mp_loads[i]); i++) {
891                                         struct berval bv;
892                                         bv.bv_val = c->log;
893                                         bv.bv_len = snprintf( bv.bv_val, sizeof( c->log ),
894                                                 SLAP_X_ORDERED_FMT "%s", i,
895                                                 mp->mp_loads[i].bv_val );
896                                         if ( bv.bv_len >= sizeof( c->log ) ) {
897                                                 ber_bvarray_free_x( c->rvalue_vals, NULL );
898                                                 c->rvalue_vals = NULL;
899                                                 break;
900                                         }
901                                         value_add_one( &c->rvalue_vals, &bv );
902                                 }
903                         }
904
905                         rc = c->rvalue_vals ? 0 : 1;
906                         }
907                         break;
908                 case CFG_MODPATH: {
909                         ModPaths *mp = c->private;
910                         if ( !BER_BVISNULL( &mp->mp_path ))
911                                 value_add_one( &c->rvalue_vals, &mp->mp_path );
912
913                         rc = c->rvalue_vals ? 0 : 1;
914                         }
915                         break;
916 #endif
917 #ifdef LDAP_SLAPI
918                 case CFG_PLUGIN:
919                         slapi_int_plugin_unparse( c->be, &c->rvalue_vals );
920                         if ( !c->rvalue_vals ) rc = 1;
921                         break;
922 #endif
923 #ifdef SLAP_AUTH_REWRITE
924                 case CFG_REWRITE:
925                         if ( authz_rewrites ) {
926                                 struct berval bv, idx;
927                                 char ibuf[32];
928                                 int i;
929
930                                 idx.bv_val = ibuf;
931                                 for ( i=0; !BER_BVISNULL( &authz_rewrites[i] ); i++ ) {
932                                         idx.bv_len = snprintf( idx.bv_val, sizeof( ibuf ), SLAP_X_ORDERED_FMT, i );
933                                         if ( idx.bv_len >= sizeof( ibuf ) ) {
934                                                 ber_bvarray_free_x( c->rvalue_vals, NULL );
935                                                 c->rvalue_vals = NULL;
936                                                 break;
937                                         }
938                                         bv.bv_len = idx.bv_len + authz_rewrites[i].bv_len;
939                                         bv.bv_val = ch_malloc( bv.bv_len + 1 );
940                                         AC_MEMCPY( bv.bv_val, idx.bv_val, idx.bv_len );
941                                         AC_MEMCPY( &bv.bv_val[ idx.bv_len ],
942                                                 authz_rewrites[i].bv_val,
943                                                 authz_rewrites[i].bv_len + 1 );
944                                         ber_bvarray_add( &c->rvalue_vals, &bv );
945                                 }
946                         }
947                         if ( !c->rvalue_vals ) rc = 1;
948                         break;
949 #endif
950                 default:
951                         rc = 1;
952                 }
953                 return rc;
954         } else if ( c->op == LDAP_MOD_DELETE ) {
955                 int rc = 0;
956                 switch(c->type) {
957                 /* single-valued attrs, no-ops */
958                 case CFG_CONCUR:
959                 case CFG_THREADS:
960                 case CFG_TTHREADS:
961                 case CFG_RO:
962                 case CFG_AZPOLICY:
963                 case CFG_DEPTH:
964                 case CFG_LASTMOD:
965                 case CFG_SASLSECP:
966                 case CFG_SSTR_IF_MAX:
967                 case CFG_SSTR_IF_MIN:
968                         break;
969
970                 /* no-ops, requires slapd restart */
971                 case CFG_PLUGIN:
972                 case CFG_MODLOAD:
973                 case CFG_AZREGEXP:
974                 case CFG_REWRITE:
975                         snprintf(c->log, sizeof( c->log ), "change requires slapd restart");
976                         break;
977
978                 case CFG_SALT:
979                         ch_free( passwd_salt );
980                         passwd_salt = NULL;
981                         break;
982
983                 case CFG_REPLOG:
984                         ch_free( c->be->be_replogfile );
985                         c->be->be_replogfile = NULL;
986                         break;
987
988                 case CFG_LOGFILE:
989                         ch_free( logfileName );
990                         logfileName = NULL;
991                         break;
992
993                 case CFG_ACL:
994                         if ( c->valx < 0 ) {
995                                 AccessControl *end;
996                                 if ( c->be == frontendDB )
997                                         end = NULL;
998                                 else
999                                         end = frontendDB->be_acl;
1000                                 acl_destroy( c->be->be_acl, end );
1001                                 c->be->be_acl = end;
1002
1003                         } else {
1004                                 AccessControl **prev, *a;
1005                                 int i;
1006                                 for (i=0, prev = &c->be->be_acl; i < c->valx;
1007                                         i++ ) {
1008                                         a = *prev;
1009                                         prev = &a->acl_next;
1010                                 }
1011                                 a = *prev;
1012                                 *prev = a->acl_next;
1013                                 acl_free( a );
1014                         }
1015                         break;
1016
1017                 case CFG_LIMITS:
1018                         /* FIXME: there is no limits_free function */
1019                 case CFG_ATOPT:
1020                         /* FIXME: there is no ad_option_free function */
1021                 case CFG_ROOTDSE:
1022                         /* FIXME: there is no way to remove attributes added by
1023                                 a DSE file */
1024                 case CFG_OID:
1025                 case CFG_OC:
1026                 case CFG_DIT:
1027                 case CFG_ATTR:
1028                 case CFG_MODPATH:
1029                 default:
1030                         rc = 1;
1031                         break;
1032                 }
1033                 return rc;
1034         }
1035
1036         p = strchr(c->line,'(' /*')'*/);
1037
1038         switch(c->type) {
1039                 case CFG_BACKEND:
1040                         if(!(c->bi = backend_info(c->argv[1]))) {
1041                                 snprintf( c->msg, sizeof( c->msg ), "<%s> failed init", c->argv[0] );
1042                                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)!\n",
1043                                         c->log, c->msg, c->argv[1] );
1044                                 return(1);
1045                         }
1046                         break;
1047
1048                 case CFG_DATABASE:
1049                         c->bi = NULL;
1050                         /* NOTE: config is always the first backend!
1051                          */
1052                         if ( !strcasecmp( c->argv[1], "config" )) {
1053                                 c->be = LDAP_STAILQ_FIRST(&backendDB);
1054                         } else if ( !strcasecmp( c->argv[1], "frontend" )) {
1055                                 c->be = frontendDB;
1056                         } else {
1057                                 c->be = backend_db_init(c->argv[1], NULL);
1058                                 if ( !c->be ) {
1059                                         snprintf( c->msg, sizeof( c->msg ), "<%s> failed init", c->argv[0] );
1060                                         Debug(LDAP_DEBUG_ANY, "%s: %s (%s)!\n",
1061                                                 c->log, c->msg, c->argv[1] );
1062                                         return(1);
1063                                 }
1064                         }
1065                         break;
1066
1067                 case CFG_CONCUR:
1068                         ldap_pvt_thread_set_concurrency(c->value_int);
1069                         break;
1070
1071                 case CFG_THREADS:
1072                         if ( slapMode & SLAP_SERVER_MODE )
1073                                 ldap_pvt_thread_pool_maxthreads(&connection_pool, c->value_int);
1074                         connection_pool_max = c->value_int;     /* save for reference */
1075                         break;
1076
1077                 case CFG_TTHREADS:
1078                         if ( slapMode & SLAP_TOOL_MODE )
1079                                 ldap_pvt_thread_pool_maxthreads(&connection_pool, c->value_int);
1080                         slap_tool_thread_max = c->value_int;    /* save for reference */
1081                         break;
1082
1083                 case CFG_SALT:
1084                         if ( passwd_salt ) ch_free( passwd_salt );
1085                         passwd_salt = c->value_string;
1086                         lutil_salt_format(passwd_salt);
1087                         break;
1088
1089                 case CFG_LIMITS:
1090                         if(limits_parse(c->be, c->fname, c->lineno, c->argc, c->argv))
1091                                 return(1);
1092                         break;
1093
1094                 case CFG_RO:
1095                         if(c->value_int)
1096                                 c->be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
1097                         else
1098                                 c->be->be_restrictops &= ~SLAP_RESTRICT_OP_WRITES;
1099                         break;
1100
1101                 case CFG_AZPOLICY:
1102                         ch_free(c->value_string);
1103                         if (slap_sasl_setpolicy( c->argv[1] )) {
1104                                 snprintf( c->msg, sizeof( c->msg ), "<%s> unable to parse value", c->argv[0] );
1105                                 Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
1106                                         c->log, c->msg, c->argv[1] );
1107                                 return(1);
1108                         }
1109                         break;
1110                 
1111                 case CFG_AZREGEXP:
1112                         if (slap_sasl_regexp_config( c->argv[1], c->argv[2] ))
1113                                 return(1);
1114                         break;
1115                                 
1116 #ifdef HAVE_CYRUS_SASL
1117                 case CFG_SASLSECP:
1118                         {
1119                         char *txt = slap_sasl_secprops( c->argv[1] );
1120                         if ( txt ) {
1121                                 snprintf( c->msg, sizeof(c->msg), "<%s> %s",
1122                                         c->argv[0], txt );
1123                                 Debug(LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
1124                                 return(1);
1125                         }
1126                         break;
1127                         }
1128 #endif
1129
1130                 case CFG_DEPTH:
1131                         c->be->be_max_deref_depth = c->value_int;
1132                         break;
1133
1134                 case CFG_OID: {
1135                         OidMacro *om;
1136
1137                         if(parse_oidm(c->fname, c->lineno, c->argc, c->argv, 1, &om))
1138                                 return(1);
1139                         if (!cfn->c_om_head) cfn->c_om_head = om;
1140                         cfn->c_om_tail = om;
1141                         }
1142                         break;
1143
1144                 case CFG_OC: {
1145                         ObjectClass *oc;
1146
1147                         if(parse_oc(c->fname, c->lineno, p, c->argv, &oc)) return(1);
1148                         if (!cfn->c_oc_head) cfn->c_oc_head = oc;
1149                         cfn->c_oc_tail = oc;
1150                         }
1151                         break;
1152
1153                 case CFG_DIT: {
1154                         ContentRule *cr;
1155
1156                         if(parse_cr(c->fname, c->lineno, p, c->argv, &cr)) return(1);
1157                         if (!cfn->c_cr_head) cfn->c_cr_head = cr;
1158                         cfn->c_cr_tail = cr;
1159                         }
1160                         break;
1161
1162                 case CFG_ATTR: {
1163                         AttributeType *at;
1164
1165                         if(parse_at(c->fname, c->lineno, p, c->argv, &at)) return(1);
1166                         if (!cfn->c_at_head) cfn->c_at_head = at;
1167                         cfn->c_at_tail = at;
1168                         }
1169                         break;
1170
1171                 case CFG_ATOPT:
1172                         ad_define_option(NULL, NULL, 0);
1173                         for(i = 1; i < c->argc; i++)
1174                                 if(ad_define_option(c->argv[i], c->fname, c->lineno))
1175                                         return(1);
1176                         break;
1177
1178                 case CFG_ACL:
1179                         if ( parse_acl(c->be, c->fname, c->lineno, c->argc, c->argv, c->valx ) ) {
1180                                 return 1;
1181                         }
1182                         break;
1183
1184                 case CFG_REPLOG:
1185                         if(SLAP_MONITOR(c->be)) {
1186                                 Debug(LDAP_DEBUG_ANY, "%s: "
1187                                         "\"replogfile\" should not be used "
1188                                         "inside monitor database\n",
1189                                         c->log, 0, 0);
1190                                 return(0);      /* FIXME: should this be an error? */
1191                         }
1192
1193                         c->be->be_replogfile = c->value_string;
1194                         break;
1195
1196                 case CFG_ROOTDSE:
1197                         if(read_root_dse_file(c->argv[1])) {
1198                                 snprintf( c->msg, sizeof( c->msg ), "<%s> could not read file", c->argv[0] );
1199                                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1200                                         c->log, c->msg, c->argv[1] );
1201                                 return(1);
1202                         }
1203                         {
1204                                 struct berval bv;
1205                                 ber_str2bv( c->argv[1], 0, 1, &bv );
1206                                 ber_bvarray_add( &cfn->c_dseFiles, &bv );
1207                         }
1208                         break;
1209
1210                 case CFG_LOGFILE: {
1211                                 FILE *logfile;
1212                                 if ( logfileName ) ch_free( logfileName );
1213                                 logfileName = c->value_string;
1214                                 logfile = fopen(logfileName, "w");
1215                                 if(logfile) lutil_debug_file(logfile);
1216                         } break;
1217
1218                 case CFG_LASTMOD:
1219                         if(SLAP_NOLASTMODCMD(c->be)) {
1220                                 snprintf( c->msg, sizeof( c->msg ), "<%s> not available for %s database",
1221                                         c->argv[0], c->be->bd_info->bi_type );
1222                                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
1223                                         c->log, c->msg, 0 );
1224                                 return(1);
1225                         }
1226                         if(c->value_int)
1227                                 SLAP_DBFLAGS(c->be) &= ~SLAP_DBFLAG_NOLASTMOD;
1228                         else
1229                                 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_NOLASTMOD;
1230                         break;
1231
1232                 case CFG_SSTR_IF_MAX:
1233                         if (c->value_int < index_substr_if_minlen) {
1234                                 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid value", c->argv[0] );
1235                                 Debug(LDAP_DEBUG_ANY, "%s: %s (%d)\n",
1236                                         c->log, c->msg, c->value_int );
1237                                 return(1);
1238                         }
1239                         index_substr_if_maxlen = c->value_int;
1240                         break;
1241
1242                 case CFG_SSTR_IF_MIN:
1243                         if (c->value_int > index_substr_if_maxlen) {
1244                                 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid value", c->argv[0] );
1245                                 Debug(LDAP_DEBUG_ANY, "%s: %s (%d)\n",
1246                                         c->log, c->msg, c->value_int );
1247                                 return(1);
1248                         }
1249                         index_substr_if_minlen = c->value_int;
1250                         break;
1251
1252 #ifdef SLAPD_MODULES
1253                 case CFG_MODLOAD:
1254                         /* If we're just adding a module on an existing modpath,
1255                          * make sure we've selected the current path.
1256                          */
1257                         if ( c->op == LDAP_MOD_ADD && c->private && modcur != c->private ) {
1258                                 modcur = c->private;
1259                                 /* This should never fail */
1260                                 if ( module_path( modcur->mp_path.bv_val )) {
1261                                         snprintf( c->msg, sizeof( c->msg ), "<%s> module path no longer valid",
1262                                                 c->argv[0] );
1263                                         Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
1264                                                 c->log, c->msg, modcur->mp_path.bv_val );
1265                                         return(1);
1266                                 }
1267                         }
1268                         if(module_load(c->argv[1], c->argc - 2, (c->argc > 2) ? c->argv + 2 : NULL))
1269                                 return(1);
1270                         /* Record this load on the current path */
1271                         {
1272                                 struct berval bv;
1273                                 char *ptr;
1274                                 if ( c->op == SLAP_CONFIG_ADD ) {
1275                                         ptr = c->line + STRLENOF("moduleload");
1276                                         while (!isspace(*ptr)) ptr++;
1277                                         while (isspace(*ptr)) ptr++;
1278                                 } else {
1279                                         ptr = c->line;
1280                                 }
1281                                 ber_str2bv(ptr, 0, 1, &bv);
1282                                 ber_bvarray_add( &modcur->mp_loads, &bv );
1283                         }
1284                         break;
1285
1286                 case CFG_MODPATH:
1287                         if(module_path(c->argv[1])) return(1);
1288                         /* Record which path was used with each module */
1289                         {
1290                                 ModPaths *mp;
1291
1292                                 if (!modpaths.mp_loads) {
1293                                         mp = &modpaths;
1294                                 } else {
1295                                         mp = ch_malloc( sizeof( ModPaths ));
1296                                         modlast->mp_next = mp;
1297                                 }
1298                                 ber_str2bv(c->argv[1], 0, 1, &mp->mp_path);
1299                                 mp->mp_next = NULL;
1300                                 mp->mp_loads = NULL;
1301                                 modlast = mp;
1302                                 c->private = mp;
1303                                 modcur = mp;
1304                         }
1305                         
1306                         break;
1307 #endif
1308
1309 #ifdef LDAP_SLAPI
1310                 case CFG_PLUGIN:
1311                         if(slapi_int_read_config(c->be, c->fname, c->lineno, c->argc, c->argv) != LDAP_SUCCESS)
1312                                 return(1);
1313                         slapi_plugins_used++;
1314                         break;
1315 #endif
1316
1317 #ifdef SLAP_AUTH_REWRITE
1318                 case CFG_REWRITE: {
1319                         struct berval bv;
1320                         char *line;
1321                         
1322                         if(slap_sasl_rewrite_config(c->fname, c->lineno, c->argc, c->argv))
1323                                 return(1);
1324
1325                         if ( c->argc > 1 ) {
1326                                 char    *s;
1327
1328                                 /* quote all args but the first */
1329                                 line = ldap_charray2str( c->argv, "\" \"" );
1330                                 ber_str2bv( line, 0, 0, &bv );
1331                                 s = ber_bvchr( &bv, '"' );
1332                                 assert( s != NULL );
1333                                 /* move the trailing quote of argv[0] to the end */
1334                                 AC_MEMCPY( s, s + 1, bv.bv_len - ( s - bv.bv_val ) );
1335                                 bv.bv_val[ bv.bv_len - 1 ] = '"';
1336
1337                         } else {
1338                                 ber_str2bv( c->argv[ 0 ], 0, 1, &bv );
1339                         }
1340                         
1341                         ber_bvarray_add( &authz_rewrites, &bv );
1342                         }
1343                         break;
1344 #endif
1345
1346
1347                 default:
1348                         Debug( SLAPD_DEBUG_CONFIG_ERROR,
1349                                 "%s: unknown CFG_TYPE %d"
1350                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
1351                                 c->log, c->type, 0 );
1352 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
1353                         return 1;
1354 #endif /* SLAPD_CONF_UNKNOWN_BAILOUT */
1355
1356         }
1357         return(0);
1358 }
1359
1360
1361 static int
1362 config_fname(ConfigArgs *c) {
1363         if(c->op == SLAP_CONFIG_EMIT) {
1364                 if (c->private) {
1365                         ConfigFile *cf = c->private;
1366                         value_add_one( &c->rvalue_vals, &cf->c_file );
1367                         return 0;
1368                 }
1369                 return 1;
1370         }
1371         return(0);
1372 }
1373
1374 static int
1375 config_cfdir(ConfigArgs *c) {
1376         if(c->op == SLAP_CONFIG_EMIT) {
1377                 if ( !BER_BVISEMPTY( &cfdir )) {
1378                         value_add_one( &c->rvalue_vals, &cfdir );
1379                         return 0;
1380                 }
1381                 return 1;
1382         }
1383         return(0);
1384 }
1385
1386 static int
1387 config_search_base(ConfigArgs *c) {
1388         if(c->op == SLAP_CONFIG_EMIT) {
1389                 int rc = 1;
1390                 if (!BER_BVISEMPTY(&default_search_base)) {
1391                         value_add_one(&c->rvalue_vals, &default_search_base);
1392                         value_add_one(&c->rvalue_nvals, &default_search_nbase);
1393                         rc = 0;
1394                 }
1395                 return rc;
1396         } else if( c->op == LDAP_MOD_DELETE ) {
1397                 ch_free( default_search_base.bv_val );
1398                 ch_free( default_search_nbase.bv_val );
1399                 BER_BVZERO( &default_search_base );
1400                 BER_BVZERO( &default_search_nbase );
1401                 return 0;
1402         }
1403
1404         if(c->bi || c->be != frontendDB) {
1405                 Debug(LDAP_DEBUG_ANY, "%s: defaultSearchBase line must appear "
1406                         "prior to any backend or database definition\n",
1407                         c->log, 0, 0);
1408                 return(1);
1409         }
1410
1411         if(default_search_nbase.bv_len) {
1412                 free(default_search_base.bv_val);
1413                 free(default_search_nbase.bv_val);
1414         }
1415
1416         default_search_base = c->value_dn;
1417         default_search_nbase = c->value_ndn;
1418         return(0);
1419 }
1420
1421 static int
1422 config_passwd_hash(ConfigArgs *c) {
1423         int i;
1424         if (c->op == SLAP_CONFIG_EMIT) {
1425                 struct berval bv;
1426                 for (i=0; default_passwd_hash && default_passwd_hash[i]; i++) {
1427                         ber_str2bv(default_passwd_hash[i], 0, 0, &bv);
1428                         value_add_one(&c->rvalue_vals, &bv);
1429                 }
1430                 return i ? 0 : 1;
1431         } else if ( c->op == LDAP_MOD_DELETE ) {
1432                 if ( c->valx < 0 ) {
1433                         ldap_charray_free( default_passwd_hash );
1434                         default_passwd_hash = NULL;
1435                 } else {
1436                         i = c->valx;
1437                         ch_free( default_passwd_hash[i] );
1438                         for (; default_passwd_hash[i]; i++ )
1439                                 default_passwd_hash[i] = default_passwd_hash[i+1];
1440                 }
1441                 return 0;
1442         }
1443         if(default_passwd_hash) {
1444                 Debug(LDAP_DEBUG_ANY, "%s: "
1445                         "already set default password_hash\n",
1446                         c->log, 0, 0);
1447                 return(1);
1448         }
1449         for(i = 1; i < c->argc; i++) {
1450                 if(!lutil_passwd_scheme(c->argv[i])) {
1451                         snprintf( c->msg, sizeof( c->msg ), "<%s> scheme not available", c->argv[0] );
1452                         Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
1453                                 c->log, c->msg, c->argv[i]);
1454                 } else {
1455                         ldap_charray_add(&default_passwd_hash, c->argv[i]);
1456                 }
1457                 if(!default_passwd_hash) {
1458                         snprintf( c->msg, sizeof( c->msg ), "<%s> no valid hashes found", c->argv[0] );
1459                         Debug(LDAP_DEBUG_ANY, "%s: %s\n",
1460                                 c->log, c->msg, 0 );
1461                         return(1);
1462                 }
1463         }
1464         return(0);
1465 }
1466
1467 static int
1468 config_schema_dn(ConfigArgs *c) {
1469         if ( c->op == SLAP_CONFIG_EMIT ) {
1470                 int rc = 1;
1471                 if ( !BER_BVISEMPTY( &c->be->be_schemadn )) {
1472                         value_add_one(&c->rvalue_vals, &c->be->be_schemadn);
1473                         value_add_one(&c->rvalue_nvals, &c->be->be_schemandn);
1474                         rc = 0;
1475                 }
1476                 return rc;
1477         } else if ( c->op == LDAP_MOD_DELETE ) {
1478                 ch_free( c->be->be_schemadn.bv_val );
1479                 ch_free( c->be->be_schemandn.bv_val );
1480                 BER_BVZERO( &c->be->be_schemadn );
1481                 BER_BVZERO( &c->be->be_schemandn );
1482                 return 0;
1483         }
1484         ch_free( c->be->be_schemadn.bv_val );
1485         ch_free( c->be->be_schemandn.bv_val );
1486         c->be->be_schemadn = c->value_dn;
1487         c->be->be_schemandn = c->value_ndn;
1488         return(0);
1489 }
1490
1491 static int
1492 config_sizelimit(ConfigArgs *c) {
1493         int i, rc = 0;
1494         struct slap_limits_set *lim = &c->be->be_def_limit;
1495         if (c->op == SLAP_CONFIG_EMIT) {
1496                 char buf[8192];
1497                 struct berval bv;
1498                 bv.bv_val = buf;
1499                 bv.bv_len = 0;
1500                 limits_unparse_one( lim, SLAP_LIMIT_SIZE, &bv );
1501                 if ( !BER_BVISEMPTY( &bv ))
1502                         value_add_one( &c->rvalue_vals, &bv );
1503                 else
1504                         rc = 1;
1505                 return rc;
1506         } else if ( c->op == LDAP_MOD_DELETE ) {
1507                 /* Reset to defaults */
1508                 lim->lms_s_soft = SLAPD_DEFAULT_SIZELIMIT;
1509                 lim->lms_s_hard = 0;
1510                 lim->lms_s_unchecked = -1;
1511                 lim->lms_s_pr = 0;
1512                 lim->lms_s_pr_hide = 0;
1513                 lim->lms_s_pr_total = 0;
1514                 return 0;
1515         }
1516         for(i = 1; i < c->argc; i++) {
1517                 if(!strncasecmp(c->argv[i], "size", 4)) {
1518                         rc = limits_parse_one(c->argv[i], lim);
1519                         if ( rc ) {
1520                                 snprintf( c->msg, sizeof( c->msg ), "<%s> unable to parse value", c->argv[0] );
1521                                 Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
1522                                         c->log, c->msg, c->argv[i]);
1523                                 return(1);
1524                         }
1525                 } else {
1526                         if(!strcasecmp(c->argv[i], "unlimited")) {
1527                                 lim->lms_s_soft = -1;
1528                         } else {
1529                                 if ( lutil_atoix( &lim->lms_s_soft, c->argv[i], 0 ) != 0 ) {
1530                                         snprintf( c->msg, sizeof( c->msg ), "<%s> unable to parse limit", c->argv[0]);
1531                                         Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
1532                                                 c->log, c->msg, c->argv[i]);
1533                                         return(1);
1534                                 }
1535                         }
1536                         lim->lms_s_hard = 0;
1537                 }
1538         }
1539         return(0);
1540 }
1541
1542 static int
1543 config_timelimit(ConfigArgs *c) {
1544         int i, rc = 0;
1545         struct slap_limits_set *lim = &c->be->be_def_limit;
1546         if (c->op == SLAP_CONFIG_EMIT) {
1547                 char buf[8192];
1548                 struct berval bv;
1549                 bv.bv_val = buf;
1550                 bv.bv_len = 0;
1551                 limits_unparse_one( lim, SLAP_LIMIT_TIME, &bv );
1552                 if ( !BER_BVISEMPTY( &bv ))
1553                         value_add_one( &c->rvalue_vals, &bv );
1554                 else
1555                         rc = 1;
1556                 return rc;
1557         } else if ( c->op == LDAP_MOD_DELETE ) {
1558                 /* Reset to defaults */
1559                 lim->lms_t_soft = SLAPD_DEFAULT_TIMELIMIT;
1560                 lim->lms_t_hard = 0;
1561                 return 0;
1562         }
1563         for(i = 1; i < c->argc; i++) {
1564                 if(!strncasecmp(c->argv[i], "time", 4)) {
1565                         rc = limits_parse_one(c->argv[i], lim);
1566                         if ( rc ) {
1567                                 snprintf( c->msg, sizeof( c->msg ), "<%s> unable to parse value", c->argv[0] );
1568                                 Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
1569                                         c->log, c->msg, c->argv[i]);
1570                                 return(1);
1571                         }
1572                 } else {
1573                         if(!strcasecmp(c->argv[i], "unlimited")) {
1574                                 lim->lms_t_soft = -1;
1575                         } else {
1576                                 if ( lutil_atoix( &lim->lms_t_soft, c->argv[i], 0 ) != 0 ) {
1577                                         snprintf( c->msg, sizeof( c->msg ), "<%s> unable to parse limit", c->argv[0]);
1578                                         Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
1579                                                 c->log, c->msg, c->argv[i]);
1580                                         return(1);
1581                                 }
1582                         }
1583                         lim->lms_t_hard = 0;
1584                 }
1585         }
1586         return(0);
1587 }
1588
1589 static int
1590 config_overlay(ConfigArgs *c) {
1591         slap_overinfo *oi;
1592         if (c->op == SLAP_CONFIG_EMIT) {
1593                 return 1;
1594         } else if ( c->op == LDAP_MOD_DELETE ) {
1595                 assert(0);
1596         }
1597         if(c->argv[1][0] == '-' && overlay_config(c->be, &c->argv[1][1])) {
1598                 /* log error */
1599                 Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: (optional) %s overlay \"%s\" configuration failed"
1600                         SLAPD_CONF_UNKNOWN_IGNORED ".\n",
1601                         c->log, c->be == frontendDB ? "global " : "", &c->argv[1][1]);
1602 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
1603                 return 1;
1604 #endif /* SLAPD_CONF_UNKNOWN_BAILOUT */
1605         } else if(overlay_config(c->be, c->argv[1])) {
1606                 return(1);
1607         }
1608         /* Setup context for subsequent config directives.
1609          * The newly added overlay is at the head of the list.
1610          */
1611         oi = (slap_overinfo *)c->be->bd_info;
1612         c->bi = &oi->oi_list->on_bi;
1613         return(0);
1614 }
1615
1616 static int
1617 config_subordinate(ConfigArgs *c)
1618 {
1619         int rc = 1;
1620         int advertise;
1621
1622         switch( c->op ) {
1623         case SLAP_CONFIG_EMIT:
1624                 if ( SLAP_GLUE_SUBORDINATE( c->be )) {
1625                         struct berval bv;
1626
1627                         bv.bv_val = SLAP_GLUE_ADVERTISE( c->be ) ? "advertise" : "TRUE";
1628                         bv.bv_len = SLAP_GLUE_ADVERTISE( c->be ) ? STRLENOF("advertise") :
1629                                 STRLENOF("TRUE");
1630
1631                         value_add_one( &c->rvalue_vals, &bv );
1632                         rc = 0;
1633                 }
1634                 break;
1635         case LDAP_MOD_DELETE:
1636                 if ( !c->line  || strcasecmp( c->line, "advertise" )) {
1637                         glue_sub_del( c->be );
1638                 } else {
1639                         SLAP_DBFLAGS( c->be ) &= ~SLAP_DBFLAG_GLUE_ADVERTISE;
1640                 }
1641                 rc = 0;
1642                 break;
1643         case LDAP_MOD_ADD:
1644         case SLAP_CONFIG_ADD:
1645                 advertise = ( c->argc == 2 && !strcasecmp( c->argv[1], "advertise" ));
1646                 rc = glue_sub_add( c->be, advertise, CONFIG_ONLINE_ADD( c ));
1647                 break;
1648         }
1649         return rc;
1650 }
1651
1652 static int
1653 config_suffix(ConfigArgs *c)
1654 {
1655         Backend *tbe;
1656         struct berval pdn, ndn;
1657         char    *notallowed = NULL;
1658
1659         if ( c->be == frontendDB ) {
1660                 notallowed = "frontend";
1661
1662         } else if ( SLAP_MONITOR(c->be) ) {
1663                 notallowed = "monitor";
1664
1665         } else if ( SLAP_CONFIG(c->be) ) {
1666                 notallowed = "config";
1667         }
1668
1669         if ( notallowed != NULL ) {
1670                 char    buf[ SLAP_TEXT_BUFLEN ] = { '\0' };
1671
1672                 switch ( c->op ) {
1673                 case LDAP_MOD_ADD:
1674                 case LDAP_MOD_DELETE:
1675                 case LDAP_MOD_REPLACE:
1676                 case LDAP_MOD_INCREMENT:
1677                 case SLAP_CONFIG_ADD:
1678                         if ( !BER_BVISNULL( &c->value_dn ) ) {
1679                                 snprintf( buf, sizeof( buf ), "<%s> ",
1680                                                 c->value_dn.bv_val );
1681                         }
1682
1683                         Debug(LDAP_DEBUG_ANY,
1684                                 "%s: suffix %snot allowed in %s database.\n",
1685                                 c->log, buf, notallowed );
1686                         break;
1687
1688                 case SLAP_CONFIG_EMIT:
1689                         /* don't complain when emitting... */
1690                         break;
1691
1692                 default:
1693                         /* FIXME: don't know what values may be valid;
1694                          * please remove assertion, or add legal values
1695                          * to either block */
1696                         assert( 0 );
1697                         break;
1698                 }
1699
1700                 return 1;
1701         }
1702
1703         if (c->op == SLAP_CONFIG_EMIT) {
1704                 if ( c->be->be_suffix == NULL
1705                                 || BER_BVISNULL( &c->be->be_suffix[0] ) )
1706                 {
1707                         return 1;
1708                 } else {
1709                         value_add( &c->rvalue_vals, c->be->be_suffix );
1710                         value_add( &c->rvalue_nvals, c->be->be_nsuffix );
1711                         return 0;
1712                 }
1713         } else if ( c->op == LDAP_MOD_DELETE ) {
1714                 if ( c->valx < 0 ) {
1715                         ber_bvarray_free( c->be->be_suffix );
1716                         ber_bvarray_free( c->be->be_nsuffix );
1717                         c->be->be_suffix = NULL;
1718                         c->be->be_nsuffix = NULL;
1719                 } else {
1720                         int i = c->valx;
1721                         ch_free( c->be->be_suffix[i].bv_val );
1722                         ch_free( c->be->be_nsuffix[i].bv_val );
1723                         for (; c->be->be_suffix[i].bv_val; i++) {
1724                                 c->be->be_suffix[i] = c->be->be_suffix[i+1];
1725                                 c->be->be_nsuffix[i] = c->be->be_nsuffix[i+1];
1726                         }
1727                 }
1728                 return 0;
1729         }
1730
1731 #ifdef SLAPD_MONITOR_DN
1732         if(!strcasecmp(c->argv[1], SLAPD_MONITOR_DN)) {
1733                 snprintf( c->msg, sizeof( c->msg ), "<%s> DN is reserved for monitoring slapd",
1734                         c->argv[0] );
1735                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
1736                         c->log, c->msg, SLAPD_MONITOR_DN);
1737                 return(1);
1738         }
1739 #endif
1740
1741         pdn = c->value_dn;
1742         ndn = c->value_ndn;
1743         tbe = select_backend(&ndn, 0, 0);
1744         if(tbe == c->be) {
1745                 Debug( SLAPD_DEBUG_CONFIG_ERROR,
1746                         "%s: suffix already served by this backend!"
1747                         SLAPD_CONF_UNKNOWN_IGNORED ".\n",
1748                         c->log, 0, 0);
1749 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
1750                 return 1;
1751 #endif /* SLAPD_CONF_UNKNOWN_BAILOUT */
1752                 free(pdn.bv_val);
1753                 free(ndn.bv_val);
1754         } else if(tbe) {
1755                 char    *type = tbe->bd_info->bi_type;
1756
1757                 if ( overlay_is_over( tbe ) ) {
1758                         slap_overinfo   *oi = (slap_overinfo *)tbe->bd_info->bi_private;
1759                         type = oi->oi_orig->bi_type;
1760                 }
1761
1762                 snprintf( c->msg, sizeof( c->msg ), "<%s> namingContext \"%s\" already served by "
1763                         "a preceding %s database serving namingContext",
1764                         c->argv[0], pdn.bv_val, type );
1765                 Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
1766                         c->log, c->msg, tbe->be_suffix[0].bv_val);
1767                 free(pdn.bv_val);
1768                 free(ndn.bv_val);
1769                 return(1);
1770         } else if(pdn.bv_len == 0 && default_search_nbase.bv_len) {
1771                 Debug(LDAP_DEBUG_ANY, "%s: suffix DN empty and default search "
1772                         "base provided \"%s\" (assuming okay)\n",
1773                         c->log, default_search_base.bv_val, 0);
1774         }
1775         ber_bvarray_add(&c->be->be_suffix, &pdn);
1776         ber_bvarray_add(&c->be->be_nsuffix, &ndn);
1777         return(0);
1778 }
1779
1780 static int
1781 config_rootdn(ConfigArgs *c) {
1782         if (c->op == SLAP_CONFIG_EMIT) {
1783                 if ( !BER_BVISNULL( &c->be->be_rootdn )) {
1784                         value_add_one(&c->rvalue_vals, &c->be->be_rootdn);
1785                         value_add_one(&c->rvalue_nvals, &c->be->be_rootndn);
1786                         return 0;
1787                 } else {
1788                         return 1;
1789                 }
1790         } else if ( c->op == LDAP_MOD_DELETE ) {
1791                 ch_free( c->be->be_rootdn.bv_val );
1792                 ch_free( c->be->be_rootndn.bv_val );
1793                 BER_BVZERO( &c->be->be_rootdn );
1794                 BER_BVZERO( &c->be->be_rootndn );
1795                 return 0;
1796         }
1797         if ( !BER_BVISNULL( &c->be->be_rootdn )) {
1798                 ch_free( c->be->be_rootdn.bv_val );
1799                 ch_free( c->be->be_rootndn.bv_val );
1800         }
1801         c->be->be_rootdn = c->value_dn;
1802         c->be->be_rootndn = c->value_ndn;
1803         return(0);
1804 }
1805
1806 static int
1807 config_rootpw(ConfigArgs *c) {
1808         Backend *tbe;
1809
1810         if (c->op == SLAP_CONFIG_EMIT) {
1811                 if (!BER_BVISEMPTY(&c->be->be_rootpw)) {
1812                         /* don't copy, because "rootpw" is marked
1813                          * as CFG_BERVAL */
1814                         c->value_bv = c->be->be_rootpw;
1815                         return 0;
1816                 }
1817                 return 1;
1818         } else if ( c->op == LDAP_MOD_DELETE ) {
1819                 ch_free( c->be->be_rootpw.bv_val );
1820                 BER_BVZERO( &c->be->be_rootpw );
1821                 return 0;
1822         }
1823
1824         tbe = select_backend(&c->be->be_rootndn, 0, 0);
1825         if(tbe != c->be) {
1826                 snprintf( c->msg, sizeof( c->msg ), "<%s> can only be set when rootdn is under suffix",
1827                         c->argv[0] );
1828                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
1829                         c->log, c->msg, 0);
1830                 return(1);
1831         }
1832         if ( !BER_BVISNULL( &c->be->be_rootpw ))
1833                 ch_free( c->be->be_rootpw.bv_val );
1834         c->be->be_rootpw = c->value_bv;
1835         return(0);
1836 }
1837
1838 static int
1839 config_restrict(ConfigArgs *c) {
1840         slap_mask_t restrictops = 0;
1841         int i;
1842         slap_verbmasks restrictable_ops[] = {
1843                 { BER_BVC("bind"),              SLAP_RESTRICT_OP_BIND },
1844                 { BER_BVC("add"),               SLAP_RESTRICT_OP_ADD },
1845                 { BER_BVC("modify"),            SLAP_RESTRICT_OP_MODIFY },
1846                 { BER_BVC("rename"),            SLAP_RESTRICT_OP_RENAME },
1847                 { BER_BVC("modrdn"),            0 },
1848                 { BER_BVC("delete"),            SLAP_RESTRICT_OP_DELETE },
1849                 { BER_BVC("search"),            SLAP_RESTRICT_OP_SEARCH },
1850                 { BER_BVC("compare"),   SLAP_RESTRICT_OP_COMPARE },
1851                 { BER_BVC("read"),              SLAP_RESTRICT_OP_READS },
1852                 { BER_BVC("write"),             SLAP_RESTRICT_OP_WRITES },
1853                 { BER_BVC("extended"),  SLAP_RESTRICT_OP_EXTENDED },
1854                 { BER_BVC("extended=" LDAP_EXOP_START_TLS ),            SLAP_RESTRICT_EXOP_START_TLS },
1855                 { BER_BVC("extended=" LDAP_EXOP_MODIFY_PASSWD ),        SLAP_RESTRICT_EXOP_MODIFY_PASSWD },
1856                 { BER_BVC("extended=" LDAP_EXOP_X_WHO_AM_I ),           SLAP_RESTRICT_EXOP_WHOAMI },
1857                 { BER_BVC("extended=" LDAP_EXOP_X_CANCEL ),             SLAP_RESTRICT_EXOP_CANCEL },
1858                 { BER_BVNULL,   0 }
1859         };
1860
1861         if (c->op == SLAP_CONFIG_EMIT) {
1862                 return mask_to_verbs( restrictable_ops, c->be->be_restrictops,
1863                         &c->rvalue_vals );
1864         } else if ( c->op == LDAP_MOD_DELETE ) {
1865                 if ( !c->line ) {
1866                         c->be->be_restrictops = 0;
1867                 } else {
1868                         restrictops = verb_to_mask( c->line, restrictable_ops );
1869                         c->be->be_restrictops ^= restrictops;
1870                 }
1871                 return 0;
1872         }
1873         i = verbs_to_mask( c->argc, c->argv, restrictable_ops, &restrictops );
1874         if ( i ) {
1875                 snprintf( c->msg, sizeof( c->msg ), "<%s> unknown operation", c->argv[0] );
1876                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1877                         c->log, c->msg, c->argv[i]);
1878                 return(1);
1879         }
1880         if ( restrictops & SLAP_RESTRICT_OP_EXTENDED )
1881                 restrictops &= ~SLAP_RESTRICT_EXOP_MASK;
1882         c->be->be_restrictops |= restrictops;
1883         return(0);
1884 }
1885
1886 static int
1887 config_allows(ConfigArgs *c) {
1888         slap_mask_t allows = 0;
1889         int i;
1890         slap_verbmasks allowable_ops[] = {
1891                 { BER_BVC("bind_v2"),           SLAP_ALLOW_BIND_V2 },
1892                 { BER_BVC("bind_anon_cred"),    SLAP_ALLOW_BIND_ANON_CRED },
1893                 { BER_BVC("bind_anon_dn"),      SLAP_ALLOW_BIND_ANON_DN },
1894                 { BER_BVC("update_anon"),       SLAP_ALLOW_UPDATE_ANON },
1895                 { BER_BVNULL,   0 }
1896         };
1897         if (c->op == SLAP_CONFIG_EMIT) {
1898                 return mask_to_verbs( allowable_ops, global_allows, &c->rvalue_vals );
1899         } else if ( c->op == LDAP_MOD_DELETE ) {
1900                 if ( !c->line ) {
1901                         global_allows = 0;
1902                 } else {
1903                         allows = verb_to_mask( c->line, allowable_ops );
1904                         global_allows ^= allows;
1905                 }
1906                 return 0;
1907         }
1908         i = verbs_to_mask(c->argc, c->argv, allowable_ops, &allows);
1909         if ( i ) {
1910                 snprintf( c->msg, sizeof( c->msg ), "<%s> unknown feature", c->argv[0] );
1911                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1912                         c->log, c->msg, c->argv[i]);
1913                 return(1);
1914         }
1915         global_allows |= allows;
1916         return(0);
1917 }
1918
1919 static int
1920 config_disallows(ConfigArgs *c) {
1921         slap_mask_t disallows = 0;
1922         int i;
1923         slap_verbmasks disallowable_ops[] = {
1924                 { BER_BVC("bind_anon"),         SLAP_DISALLOW_BIND_ANON },
1925                 { BER_BVC("bind_simple"),       SLAP_DISALLOW_BIND_SIMPLE },
1926                 { BER_BVC("bind_krb4"),         SLAP_DISALLOW_BIND_KRBV4 },
1927                 { BER_BVC("tls_2_anon"),                SLAP_DISALLOW_TLS_2_ANON },
1928                 { BER_BVC("tls_authc"),         SLAP_DISALLOW_TLS_AUTHC },
1929                 { BER_BVNULL, 0 }
1930         };
1931         if (c->op == SLAP_CONFIG_EMIT) {
1932                 return mask_to_verbs( disallowable_ops, global_disallows, &c->rvalue_vals );
1933         } else if ( c->op == LDAP_MOD_DELETE ) {
1934                 if ( !c->line ) {
1935                         global_disallows = 0;
1936                 } else {
1937                         disallows = verb_to_mask( c->line, disallowable_ops );
1938                         global_disallows ^= disallows;
1939                 }
1940                 return 0;
1941         }
1942         i = verbs_to_mask(c->argc, c->argv, disallowable_ops, &disallows);
1943         if ( i ) {
1944                 snprintf( c->msg, sizeof( c->msg ), "<%s> unknown feature", c->argv[0] );
1945                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1946                         c->log, c->msg, c->argv[i]);
1947                 return(1);
1948         }
1949         global_disallows |= disallows;
1950         return(0);
1951 }
1952
1953 static int
1954 config_requires(ConfigArgs *c) {
1955         slap_mask_t requires = 0;
1956         int i;
1957         slap_verbmasks requires_ops[] = {
1958                 { BER_BVC("bind"),              SLAP_REQUIRE_BIND },
1959                 { BER_BVC("LDAPv3"),            SLAP_REQUIRE_LDAP_V3 },
1960                 { BER_BVC("authc"),             SLAP_REQUIRE_AUTHC },
1961                 { BER_BVC("sasl"),              SLAP_REQUIRE_SASL },
1962                 { BER_BVC("strong"),            SLAP_REQUIRE_STRONG },
1963                 { BER_BVNULL, 0 }
1964         };
1965         if (c->op == SLAP_CONFIG_EMIT) {
1966                 return mask_to_verbs( requires_ops, c->be->be_requires, &c->rvalue_vals );
1967         } else if ( c->op == LDAP_MOD_DELETE ) {
1968                 if ( !c->line ) {
1969                         c->be->be_requires = 0;
1970                 } else {
1971                         requires = verb_to_mask( c->line, requires_ops );
1972                         c->be->be_requires ^= requires;
1973                 }
1974                 return 0;
1975         }
1976         i = verbs_to_mask(c->argc, c->argv, requires_ops, &requires);
1977         if ( i ) {
1978                 snprintf( c->msg, sizeof( c->msg ), "<%s> unknown feature", c->argv[0] );
1979                 Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
1980                         c->log, c->msg, c->argv[i]);
1981                 return(1);
1982         }
1983         c->be->be_requires = requires;
1984         return(0);
1985 }
1986
1987 static slap_verbmasks   *loglevel_ops;
1988
1989 static int
1990 loglevel_init( void )
1991 {
1992         slap_verbmasks  lo[] = {
1993                 { BER_BVC("Any"),       -1 },
1994                 { BER_BVC("Trace"),     LDAP_DEBUG_TRACE },
1995                 { BER_BVC("Packets"),   LDAP_DEBUG_PACKETS },
1996                 { BER_BVC("Args"),      LDAP_DEBUG_ARGS },
1997                 { BER_BVC("Conns"),     LDAP_DEBUG_CONNS },
1998                 { BER_BVC("BER"),       LDAP_DEBUG_BER },
1999                 { BER_BVC("Filter"),    LDAP_DEBUG_FILTER },
2000                 { BER_BVC("Config"),    LDAP_DEBUG_CONFIG },
2001                 { BER_BVC("ACL"),       LDAP_DEBUG_ACL },
2002                 { BER_BVC("Stats"),     LDAP_DEBUG_STATS },
2003                 { BER_BVC("Stats2"),    LDAP_DEBUG_STATS2 },
2004                 { BER_BVC("Shell"),     LDAP_DEBUG_SHELL },
2005                 { BER_BVC("Parse"),     LDAP_DEBUG_PARSE },
2006                 { BER_BVC("Cache"),     LDAP_DEBUG_CACHE },
2007                 { BER_BVC("Index"),     LDAP_DEBUG_INDEX },
2008                 { BER_BVC("Sync"),      LDAP_DEBUG_SYNC },
2009                 { BER_BVC("None"),      LDAP_DEBUG_NONE },
2010                 { BER_BVNULL,           0 }
2011         };
2012
2013         return slap_verbmasks_init( &loglevel_ops, lo );
2014 }
2015
2016 static void
2017 loglevel_destroy( void )
2018 {
2019         if ( loglevel_ops ) {
2020                 (void)slap_verbmasks_destroy( loglevel_ops );
2021         }
2022         loglevel_ops = NULL;
2023 }
2024
2025 static slap_mask_t      loglevel_ignore[] = { -1, 0 };
2026
2027 int
2028 slap_loglevel_register( slap_mask_t m, struct berval *s )
2029 {
2030         int     rc;
2031
2032         if ( loglevel_ops == NULL ) {
2033                 loglevel_init();
2034         }
2035
2036         rc = slap_verbmasks_append( &loglevel_ops, m, s, loglevel_ignore );
2037
2038         if ( rc != 0 ) {
2039                 Debug( LDAP_DEBUG_ANY, "slap_loglevel_register(%lu, \"%s\") failed\n",
2040                         m, s->bv_val, 0 );
2041         }
2042
2043         return rc;
2044 }
2045
2046 int
2047 slap_loglevel_get( struct berval *s, int *l )
2048 {
2049         int             rc;
2050         unsigned long   i;
2051         slap_mask_t     m;
2052
2053         if ( loglevel_ops == NULL ) {
2054                 loglevel_init();
2055         }
2056
2057         for ( m = 0, i = 1; !BER_BVISNULL( &loglevel_ops[ i ].word ); i++ ) {
2058                 m |= loglevel_ops[ i ].mask;
2059         }
2060
2061         m = ~m;
2062
2063         for ( i = 1; i <= ( 1 << ( sizeof( int ) * 8 - 1 ) ) && !( m & i ); i <<= 1 )
2064                 ;
2065
2066         if ( !( m & i ) ) {
2067                 return -1;
2068         }
2069
2070         rc = slap_verbmasks_append( &loglevel_ops, i, s, loglevel_ignore );
2071
2072         if ( rc != 0 ) {
2073                 Debug( LDAP_DEBUG_ANY, "slap_loglevel_register(%lu, \"%s\") failed\n",
2074                         i, s->bv_val, 0 );
2075
2076         } else {
2077                 *l = i;
2078         }
2079
2080         return rc;
2081 }
2082
2083 int
2084 str2loglevel( const char *s, int *l )
2085 {
2086         int     i;
2087
2088         if ( loglevel_ops == NULL ) {
2089                 loglevel_init();
2090         }
2091
2092         i = verb_to_mask( s, loglevel_ops );
2093
2094         if ( BER_BVISNULL( &loglevel_ops[ i ].word ) ) {
2095                 return -1;
2096         }
2097
2098         *l = loglevel_ops[ i ].mask;
2099
2100         return 0;
2101 }
2102
2103 const char *
2104 loglevel2str( int l )
2105 {
2106         struct berval   bv = BER_BVNULL;
2107
2108         loglevel2bv( l, &bv );
2109
2110         return bv.bv_val;
2111 }
2112
2113 int
2114 loglevel2bv( int l, struct berval *bv )
2115 {
2116         if ( loglevel_ops == NULL ) {
2117                 loglevel_init();
2118         }
2119
2120         BER_BVZERO( bv );
2121
2122         return enum_to_verb( loglevel_ops, l, bv ) == -1;
2123 }
2124
2125 int
2126 loglevel2bvarray( int l, BerVarray *bva )
2127 {
2128         if ( loglevel_ops == NULL ) {
2129                 loglevel_init();
2130         }
2131
2132         return mask_to_verbs( loglevel_ops, l, bva );
2133 }
2134
2135 static int config_syslog;
2136
2137 static int
2138 config_loglevel(ConfigArgs *c) {
2139         int i;
2140
2141         if ( loglevel_ops == NULL ) {
2142                 loglevel_init();
2143         }
2144
2145         if (c->op == SLAP_CONFIG_EMIT) {
2146                 /* Get default or commandline slapd setting */
2147                 if ( ldap_syslog && !config_syslog )
2148                         config_syslog = ldap_syslog;
2149                 return loglevel2bvarray( config_syslog, &c->rvalue_vals );
2150
2151         } else if ( c->op == LDAP_MOD_DELETE ) {
2152                 if ( !c->line ) {
2153                         config_syslog = 0;
2154                 } else {
2155                         int level = verb_to_mask( c->line, loglevel_ops );
2156                         config_syslog ^= level;
2157                 }
2158                 if ( slapMode & SLAP_SERVER_MODE ) {
2159                         ldap_syslog = config_syslog;
2160                 }
2161                 return 0;
2162         }
2163
2164         config_syslog = 0;
2165
2166         for( i=1; i < c->argc; i++ ) {
2167                 int     level;
2168
2169                 if ( isdigit( c->argv[i][0] ) || c->argv[i][0] == '-' ) {
2170                         if( lutil_atoi( &level, c->argv[i] ) != 0 ) {
2171                                 snprintf( c->msg, sizeof( c->msg ), "<%s> unable to parse level", c->argv[0] );
2172                                 Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2173                                         c->log, c->msg, c->argv[i]);
2174                                 return( 1 );
2175                         }
2176                 } else {
2177                         if ( str2loglevel( c->argv[i], &level ) ) {
2178                                 snprintf( c->msg, sizeof( c->msg ), "<%s> unknown level", c->argv[0] );
2179                                 Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2180                                         c->log, c->msg, c->argv[i]);
2181                                 return( 1 );
2182                         }
2183                 }
2184                 config_syslog |= level;
2185         }
2186         if ( slapMode & SLAP_SERVER_MODE ) {
2187                 ldap_syslog = config_syslog;
2188         }
2189         return(0);
2190 }
2191
2192 static int
2193 config_referral(ConfigArgs *c) {
2194         struct berval val;
2195         if (c->op == SLAP_CONFIG_EMIT) {
2196                 if ( default_referral ) {
2197                         value_add( &c->rvalue_vals, default_referral );
2198                         return 0;
2199                 } else {
2200                         return 1;
2201                 }
2202         } else if ( c->op == LDAP_MOD_DELETE ) {
2203                 if ( c->valx < 0 ) {
2204                         ber_bvarray_free( default_referral );
2205                         default_referral = NULL;
2206                 } else {
2207                         int i = c->valx;
2208                         ch_free( default_referral[i].bv_val );
2209                         for (; default_referral[i].bv_val; i++ )
2210                                 default_referral[i] = default_referral[i+1];
2211                 }
2212                 return 0;
2213         }
2214         if(validate_global_referral(c->argv[1])) {
2215                 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid URL", c->argv[0] );
2216                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
2217                         c->log, c->msg, c->argv[1]);
2218                 return(1);
2219         }
2220
2221         ber_str2bv(c->argv[1], 0, 0, &val);
2222         if(value_add_one(&default_referral, &val)) return(LDAP_OTHER);
2223         return(0);
2224 }
2225
2226 static struct {
2227         struct berval key;
2228         int off;
2229 } sec_keys[] = {
2230         { BER_BVC("ssf="), offsetof(slap_ssf_set_t, sss_ssf) },
2231         { BER_BVC("transport="), offsetof(slap_ssf_set_t, sss_transport) },
2232         { BER_BVC("tls="), offsetof(slap_ssf_set_t, sss_tls) },
2233         { BER_BVC("sasl="), offsetof(slap_ssf_set_t, sss_sasl) },
2234         { BER_BVC("update_ssf="), offsetof(slap_ssf_set_t, sss_update_ssf) },
2235         { BER_BVC("update_transport="), offsetof(slap_ssf_set_t, sss_update_transport) },
2236         { BER_BVC("update_tls="), offsetof(slap_ssf_set_t, sss_update_tls) },
2237         { BER_BVC("update_sasl="), offsetof(slap_ssf_set_t, sss_update_sasl) },
2238         { BER_BVC("simple_bind="), offsetof(slap_ssf_set_t, sss_simple_bind) },
2239         { BER_BVNULL, 0 }
2240 };
2241
2242 static int
2243 config_security(ConfigArgs *c) {
2244         slap_ssf_set_t *set = &c->be->be_ssf_set;
2245         char *next;
2246         int i, j;
2247         if (c->op == SLAP_CONFIG_EMIT) {
2248                 char numbuf[32];
2249                 struct berval bv;
2250                 slap_ssf_t *tgt;
2251                 int rc = 1;
2252
2253                 for (i=0; !BER_BVISNULL( &sec_keys[i].key ); i++) {
2254                         tgt = (slap_ssf_t *)((char *)set + sec_keys[i].off);
2255                         if ( *tgt ) {
2256                                 rc = 0;
2257                                 bv.bv_len = snprintf( numbuf, sizeof( numbuf ), "%u", *tgt );
2258                                 if ( bv.bv_len >= sizeof( numbuf ) ) {
2259                                         ber_bvarray_free_x( c->rvalue_vals, NULL );
2260                                         c->rvalue_vals = NULL;
2261                                         rc = 1;
2262                                         break;
2263                                 }
2264                                 bv.bv_len += sec_keys[i].key.bv_len;
2265                                 bv.bv_val = ch_malloc( bv.bv_len + 1);
2266                                 next = lutil_strcopy( bv.bv_val, sec_keys[i].key.bv_val );
2267                                 strcpy( next, numbuf );
2268                                 ber_bvarray_add( &c->rvalue_vals, &bv );
2269                         }
2270                 }
2271                 return rc;
2272         }
2273         for(i = 1; i < c->argc; i++) {
2274                 slap_ssf_t *tgt = NULL;
2275                 char *src;
2276                 for ( j=0; !BER_BVISNULL( &sec_keys[j].key ); j++ ) {
2277                         if(!strncasecmp(c->argv[i], sec_keys[j].key.bv_val,
2278                                 sec_keys[j].key.bv_len)) {
2279                                 src = c->argv[i] + sec_keys[j].key.bv_len;
2280                                 tgt = (slap_ssf_t *)((char *)set + sec_keys[j].off);
2281                                 break;
2282                         }
2283                 }
2284                 if ( !tgt ) {
2285                         snprintf( c->msg, sizeof( c->msg ), "<%s> unknown factor", c->argv[0] );
2286                         Debug(LDAP_DEBUG_ANY, "%s: %s %s\n",
2287                                 c->log, c->msg, c->argv[i]);
2288                         return(1);
2289                 }
2290
2291                 if ( lutil_atou( tgt, src ) != 0 ) {
2292                         snprintf( c->msg, sizeof( c->msg ), "<%s> unable to parse factor", c->argv[0] );
2293                         Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2294                                 c->log, c->msg, c->argv[i]);
2295                         return(1);
2296                 }
2297         }
2298         return(0);
2299 }
2300
2301 char *
2302 anlist_unparse( AttributeName *an, char *ptr ) {
2303         int comma = 0;
2304
2305         for (; !BER_BVISNULL( &an->an_name ); an++) {
2306                 if ( comma ) *ptr++ = ',';
2307                 ptr = lutil_strcopy( ptr, an->an_name.bv_val );
2308                 comma = 1;
2309         }
2310         return ptr;
2311 }
2312
2313 static void
2314 replica_unparse( struct slap_replica_info *ri, int i, struct berval *bv )
2315 {
2316         int len;
2317         char *ptr;
2318         struct berval bc = BER_BVNULL;
2319         char numbuf[32];
2320
2321         assert( !BER_BVISNULL( &ri->ri_bindconf.sb_uri ) );
2322         
2323         BER_BVZERO( bv );
2324
2325         len = snprintf(numbuf, sizeof( numbuf ), SLAP_X_ORDERED_FMT, i );
2326         if ( len >= sizeof( numbuf ) ) {
2327                 /* FIXME: how can indicate error? */
2328                 return;
2329         }
2330
2331         if ( ri->ri_nsuffix ) {
2332                 for (i=0; !BER_BVISNULL( &ri->ri_nsuffix[i] ); i++) {
2333                         len += ri->ri_nsuffix[i].bv_len + STRLENOF(" suffix=\"\"");
2334                 }
2335         }
2336         if ( ri->ri_attrs ) {
2337                 len += STRLENOF(" attrs");
2338                 if ( ri->ri_exclude ) len++;
2339                 for (i=0; !BER_BVISNULL( &ri->ri_attrs[i].an_name ); i++) {
2340                         len += 1 + ri->ri_attrs[i].an_name.bv_len;
2341                 }
2342         }
2343         bindconf_unparse( &ri->ri_bindconf, &bc );
2344         len += bc.bv_len;
2345
2346         bv->bv_val = ch_malloc(len + 1);
2347         bv->bv_len = len;
2348
2349         ptr = lutil_strcopy( bv->bv_val, numbuf );
2350
2351         /* start with URI from bindconf */
2352         assert( !BER_BVISNULL( &bc ) );
2353         if ( bc.bv_val ) {
2354                 strcpy( ptr, bc.bv_val );
2355                 ch_free( bc.bv_val );
2356         }
2357
2358         if ( ri->ri_nsuffix ) {
2359                 for (i=0; !BER_BVISNULL( &ri->ri_nsuffix[i] ); i++) {
2360                         ptr = lutil_strcopy( ptr, " suffix=\"" );
2361                         ptr = lutil_strcopy( ptr, ri->ri_nsuffix[i].bv_val );
2362                         *ptr++ = '"';
2363                 }
2364         }
2365         if ( ri->ri_attrs ) {
2366                 ptr = lutil_strcopy( ptr, " attrs" );
2367                 if ( ri->ri_exclude ) *ptr++ = '!';
2368                 *ptr++ = '=';
2369                 ptr = anlist_unparse( ri->ri_attrs, ptr );
2370         }
2371 }
2372
2373 static int
2374 config_replica(ConfigArgs *c) {
2375         int i, nr = -1;
2376         char *replicahost = NULL, *replicauri = NULL;
2377         LDAPURLDesc *ludp;
2378
2379         if (c->op == SLAP_CONFIG_EMIT) {
2380                 if (c->be->be_replica) {
2381                         struct berval bv;
2382                         for (i=0;c->be->be_replica[i]; i++) {
2383                                 replica_unparse( c->be->be_replica[i], i, &bv );
2384                                 ber_bvarray_add( &c->rvalue_vals, &bv );
2385                         }
2386                         return 0;
2387                 }
2388                 return 1;
2389         } else if ( c->op == LDAP_MOD_DELETE ) {
2390                 /* FIXME: there is no replica_free function */
2391                 if ( c->valx < 0 ) {
2392                 } else {
2393                 }
2394         }
2395         if(SLAP_MONITOR(c->be)) {
2396                 Debug(LDAP_DEBUG_ANY, "%s: "
2397                         "\"replica\" should not be used inside monitor database\n",
2398                         c->log, 0, 0);
2399                 return(0);      /* FIXME: should this be an error? */
2400         }
2401
2402         for(i = 1; i < c->argc; i++) {
2403                 if(!strncasecmp(c->argv[i], "host=", STRLENOF("host="))) {
2404                         ber_len_t       len;
2405
2406                         if ( replicauri ) {
2407                                 snprintf( c->msg, sizeof( c->msg ), "<%s> replica host/URI already specified", c->argv[0] );
2408                                 Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n", c->log, c->msg, replicauri );
2409                                 return(1);
2410                         }
2411
2412                         replicahost = c->argv[i] + STRLENOF("host=");
2413                         len = strlen( replicahost ) + STRLENOF("ldap://");
2414                         replicauri = ch_malloc( len + 1 );
2415                         snprintf( replicauri, len + 1, "ldap://%s", replicahost );
2416                         replicahost = replicauri + STRLENOF( "ldap://");
2417                         nr = add_replica_info(c->be, replicauri, replicahost);
2418                         break;
2419                 } else if(!strncasecmp(c->argv[i], "uri=", STRLENOF("uri="))) {
2420                         if ( replicauri ) {
2421                                 snprintf( c->msg, sizeof( c->msg ), "<%s> replica host/URI already specified", c->argv[0] );
2422                                 Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n", c->log, c->msg, replicauri );
2423                                 return(1);
2424                         }
2425
2426                         if(ldap_url_parse(c->argv[i] + STRLENOF("uri="), &ludp) != LDAP_SUCCESS) {
2427                                 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid uri", c->argv[0] );
2428                                 Debug(LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
2429                                 return(1);
2430                         }
2431                         if(!ludp->lud_host) {
2432                                 ldap_free_urldesc(ludp);
2433                                 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid uri - missing hostname",
2434                                         c->argv[0] );
2435                                 Debug(LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
2436                                 return(1);
2437                         }
2438                         ldap_free_urldesc(ludp);
2439                         replicauri = c->argv[i] + STRLENOF("uri=");
2440                         replicauri = ch_strdup( replicauri );
2441                         replicahost = strchr( replicauri, '/' );
2442                         replicahost += 2;
2443                         nr = add_replica_info(c->be, replicauri, replicahost);
2444                         break;
2445                 }
2446         }
2447         if(i == c->argc) {
2448                 snprintf( c->msg, sizeof( c->msg ), "<%s> missing host or uri", c->argv[0] );
2449                 Debug(LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
2450                 return(1);
2451         } else if(nr == -1) {
2452                 snprintf( c->msg, sizeof( c->msg ), "<%s> unable to add replica", c->argv[0] );
2453                 Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n", c->log, c->msg,
2454                         replicauri ? replicauri : "" );
2455                 return(1);
2456         } else {
2457                 for(i = 1; i < c->argc; i++) {
2458                         if(!strncasecmp(c->argv[i], "uri=", STRLENOF("uri="))) {
2459                                 /* dealt with separately; don't let it get to bindconf */
2460                                 ;
2461
2462                         } else if(!strncasecmp(c->argv[i], "suffix=", STRLENOF( "suffix="))) {
2463                                 switch(add_replica_suffix(c->be, nr, c->argv[i] + STRLENOF("suffix="))) {
2464                                         case 1:
2465                                                 Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
2466                                                 "suffix \"%s\" in \"replica\" line is not valid for backend"
2467                                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
2468                                                 c->log, c->argv[i] + STRLENOF("suffix="), 0);
2469 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
2470                                                 return 1;
2471 #endif /* SLAPD_CONF_UNKNOWN_BAILOUT */
2472                                                 break;
2473                                         case 2:
2474                                                 Debug( SLAPD_DEBUG_CONFIG_ERROR, "%s: "
2475                                                 "unable to normalize suffix in \"replica\" line"
2476                                                 SLAPD_CONF_UNKNOWN_IGNORED ".\n",
2477                                                 c->log, 0, 0);
2478 #ifdef SLAPD_CONF_UNKNOWN_BAILOUT
2479                                                 return 1;
2480 #endif /* SLAPD_CONF_UNKNOWN_BAILOUT */
2481                                                 break;
2482                                 }
2483
2484                         } else if (!strncasecmp(c->argv[i], "attr", STRLENOF("attr"))
2485                                 || !strncasecmp(c->argv[i], "attrs", STRLENOF("attrs")))
2486                         {
2487                                 int exclude = 0;
2488                                 char *arg = c->argv[i] + STRLENOF("attr");
2489                                 if (arg[0] == 's') {
2490                                         arg++;
2491                                 } else {
2492                                         Debug( LDAP_DEBUG_ANY,
2493                                                 "%s: \"attr\" "
2494                                                 "is deprecated (and undocumented); "
2495                                                 "use \"attrs\" instead.\n",
2496                                                 c->log, 0, 0 );
2497                                 }
2498                                 if(arg[0] == '!') {
2499                                         arg++;
2500                                         exclude = 1;
2501                                 }
2502                                 if(arg[0] != '=') {
2503                                         continue;
2504                                 }
2505                                 if(add_replica_attrs(c->be, nr, arg + 1, exclude)) {
2506                                         snprintf( c->msg, sizeof( c->msg ), "<%s> unknown attribute", c->argv[0] );
2507                                         Debug(LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
2508                                                 c->log, c->msg, arg + 1);
2509                                         return(1);
2510                                 }
2511                         } else if ( bindconf_parse( c->argv[i],
2512                                         &c->be->be_replica[nr]->ri_bindconf ) ) {
2513                                 return(1);
2514                         }
2515                 }
2516         }
2517         return(0);
2518 }
2519
2520 static int
2521 config_updatedn(ConfigArgs *c) {
2522         if (c->op == SLAP_CONFIG_EMIT) {
2523                 if (!BER_BVISEMPTY(&c->be->be_update_ndn)) {
2524                         value_add_one(&c->rvalue_vals, &c->be->be_update_ndn);
2525                         value_add_one(&c->rvalue_nvals, &c->be->be_update_ndn);
2526                         return 0;
2527                 }
2528                 return 1;
2529         } else if ( c->op == LDAP_MOD_DELETE ) {
2530                 ch_free( c->be->be_update_ndn.bv_val );
2531                 BER_BVZERO( &c->be->be_update_ndn );
2532                 SLAP_DBFLAGS(c->be) ^= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SLURP_SHADOW);
2533                 return 0;
2534         }
2535         if(SLAP_SHADOW(c->be)) {
2536                 snprintf( c->msg, sizeof( c->msg ), "<%s> database already shadowed", c->argv[0] );
2537                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
2538                         c->log, c->msg, 0);
2539                 return(1);
2540         }
2541
2542         ber_memfree_x( c->value_dn.bv_val, NULL );
2543         if ( !BER_BVISNULL( &c->be->be_update_ndn ) ) {
2544                 ber_memfree_x( c->be->be_update_ndn.bv_val, NULL );
2545         }
2546         c->be->be_update_ndn = c->value_ndn;
2547         BER_BVZERO( &c->value_dn );
2548         BER_BVZERO( &c->value_ndn );
2549
2550         return config_slurp_shadow( c );
2551 }
2552
2553 int
2554 config_shadow( ConfigArgs *c, int flag )
2555 {
2556         char    *notallowed = NULL;
2557
2558         if ( c->be == frontendDB ) {
2559                 notallowed = "frontend";
2560
2561         } else if ( SLAP_MONITOR(c->be) ) {
2562                 notallowed = "monitor";
2563
2564         } else if ( SLAP_CONFIG(c->be) ) {
2565                 notallowed = "config";
2566         }
2567
2568         if ( notallowed != NULL ) {
2569                 Debug( LDAP_DEBUG_ANY, "%s: %s database cannot be shadow.\n", c->log, notallowed, 0 );
2570                 return 1;
2571         }
2572
2573         SLAP_DBFLAGS(c->be) |= (SLAP_DBFLAG_SHADOW | flag);
2574
2575         return 0;
2576 }
2577
2578 static int
2579 config_updateref(ConfigArgs *c) {
2580         struct berval val;
2581         if (c->op == SLAP_CONFIG_EMIT) {
2582                 if ( c->be->be_update_refs ) {
2583                         value_add( &c->rvalue_vals, c->be->be_update_refs );
2584                         return 0;
2585                 } else {
2586                         return 1;
2587                 }
2588         } else if ( c->op == LDAP_MOD_DELETE ) {
2589                 if ( c->valx < 0 ) {
2590                         ber_bvarray_free( c->be->be_update_refs );
2591                         c->be->be_update_refs = NULL;
2592                 } else {
2593                         int i = c->valx;
2594                         ch_free( c->be->be_update_refs[i].bv_val );
2595                         for (; c->be->be_update_refs[i].bv_val; i++)
2596                                 c->be->be_update_refs[i] = c->be->be_update_refs[i+1];
2597                 }
2598                 return 0;
2599         }
2600         if(!SLAP_SHADOW(c->be)) {
2601                 snprintf( c->msg, sizeof( c->msg ), "<%s> must appear after syncrepl or updatedn",
2602                         c->argv[0] );
2603                 Debug(LDAP_DEBUG_ANY, "%s: %s\n",
2604                         c->log, c->msg, 0);
2605                 return(1);
2606         }
2607
2608         if(validate_global_referral(c->argv[1])) {
2609                 snprintf( c->msg, sizeof( c->msg ), "<%s> invalid URL", c->argv[0] );
2610                 Debug(LDAP_DEBUG_ANY, "%s: %s (%s)\n",
2611                         c->log, c->msg, c->argv[1]);
2612                 return(1);
2613         }
2614         ber_str2bv(c->argv[1], 0, 0, &val);
2615         if(value_add_one(&c->be->be_update_refs, &val)) return(LDAP_OTHER);
2616         return(0);
2617 }
2618
2619 static int
2620 config_include(ConfigArgs *c) {
2621         int savelineno = c->lineno;
2622         int rc;
2623         ConfigFile *cf;
2624         ConfigFile *cfsave = cfn;
2625         ConfigFile *cf2 = NULL;
2626         if (c->op == SLAP_CONFIG_EMIT) {
2627                 if (c->private) {
2628                         ConfigFile *cf = c->private;
2629                         value_add_one( &c->rvalue_vals, &cf->c_file );
2630                         return 0;
2631                 }
2632                 return 1;
2633         } else if ( c->op == LDAP_MOD_DELETE ) {
2634         }
2635         cf = ch_calloc( 1, sizeof(ConfigFile));
2636         if ( cfn->c_kids ) {
2637                 for (cf2=cfn->c_kids; cf2 && cf2->c_sibs; cf2=cf2->c_sibs) ;
2638                 cf2->c_sibs = cf;
2639         } else {
2640                 cfn->c_kids = cf;
2641         }
2642         cfn = cf;
2643         ber_str2bv( c->argv[1], 0, 1, &cf->c_file );
2644         rc = read_config_file(c->argv[1], c->depth + 1, c, config_back_cf_table);
2645         c->lineno = savelineno - 1;
2646         cfn = cfsave;
2647         if ( rc ) {
2648                 if ( cf2 ) cf2->c_sibs = NULL;
2649                 else cfn->c_kids = NULL;
2650                 ch_free( cf->c_file.bv_val );
2651                 ch_free( cf );
2652         } else {
2653                 c->private = cf;
2654         }
2655         return(rc);
2656 }
2657
2658 #ifdef HAVE_TLS
2659 static int
2660 config_tls_option(ConfigArgs *c) {
2661         int flag;
2662         switch(c->type) {
2663         case CFG_TLS_RAND:      flag = LDAP_OPT_X_TLS_RANDOM_FILE;      break;
2664         case CFG_TLS_CIPHER:    flag = LDAP_OPT_X_TLS_CIPHER_SUITE;     break;
2665         case CFG_TLS_CERT_FILE: flag = LDAP_OPT_X_TLS_CERTFILE;         break;  
2666         case CFG_TLS_CERT_KEY:  flag = LDAP_OPT_X_TLS_KEYFILE;          break;
2667         case CFG_TLS_CA_PATH:   flag = LDAP_OPT_X_TLS_CACERTDIR;        break;
2668         case CFG_TLS_CA_FILE:   flag = LDAP_OPT_X_TLS_CACERTFILE;       break;
2669         case CFG_TLS_DH_FILE:   flag = LDAP_OPT_X_TLS_DHFILE;   break;
2670         default:                Debug(LDAP_DEBUG_ANY, "%s: "
2671                                         "unknown tls_option <0x%x>\n",
2672                                         c->log, c->type, 0);
2673                 return 1;
2674         }
2675         if (c->op == SLAP_CONFIG_EMIT) {
2676                 return ldap_pvt_tls_get_option( NULL, flag, &c->value_string );
2677         } else if ( c->op == LDAP_MOD_DELETE ) {
2678                 return ldap_pvt_tls_set_option( NULL, flag, NULL );
2679         }
2680         ch_free(c->value_string);
2681         return(ldap_pvt_tls_set_option(NULL, flag, c->argv[1]));
2682 }
2683
2684 /* FIXME: this ought to be provided by libldap */
2685 static int
2686 config_tls_config(ConfigArgs *c) {
2687         int i, flag;
2688         slap_verbmasks crlkeys[] = {
2689                 { BER_BVC("none"),      LDAP_OPT_X_TLS_CRL_NONE },
2690                 { BER_BVC("peer"),      LDAP_OPT_X_TLS_CRL_PEER },
2691                 { BER_BVC("all"),       LDAP_OPT_X_TLS_CRL_ALL },
2692                 { BER_BVNULL, 0 }
2693         };
2694         slap_verbmasks vfykeys[] = {
2695                 { BER_BVC("never"),     LDAP_OPT_X_TLS_NEVER },
2696                 { BER_BVC("demand"),    LDAP_OPT_X_TLS_DEMAND },
2697                 { BER_BVC("try"),       LDAP_OPT_X_TLS_TRY },
2698                 { BER_BVC("hard"),      LDAP_OPT_X_TLS_HARD },
2699                 { BER_BVNULL, 0 }
2700         }, *keys;
2701         switch(c->type) {
2702         case CFG_TLS_CRLCHECK:  flag = LDAP_OPT_X_TLS_CRLCHECK;         keys = crlkeys; break;
2703         case CFG_TLS_VERIFY:    flag = LDAP_OPT_X_TLS_REQUIRE_CERT;     keys = vfykeys; break;
2704         default:
2705                 Debug(LDAP_DEBUG_ANY, "%s: "
2706                                 "unknown tls_option <0x%x>\n",
2707                                 c->log, c->type, 0);
2708                 return 1;
2709         }
2710         if (c->op == SLAP_CONFIG_EMIT) {
2711                 ldap_pvt_tls_get_option( NULL, flag, &c->value_int );
2712                 for (i=0; !BER_BVISNULL(&keys[i].word); i++) {
2713                         if (keys[i].mask == c->value_int) {
2714                                 c->value_string = ch_strdup( keys[i].word.bv_val );
2715                                 return 0;
2716                         }
2717                 }
2718                 return 1;
2719         } else if ( c->op == LDAP_MOD_DELETE ) {
2720                 int i = 0;
2721                 return ldap_pvt_tls_set_option( NULL, flag, &i );
2722         }
2723         ch_free( c->value_string );
2724         if ( isdigit( (unsigned char)c->argv[1][0] ) ) {
2725                 if ( lutil_atoi( &i, c->argv[1] ) != 0 ) {
2726                         Debug(LDAP_DEBUG_ANY, "%s: "
2727                                 "unable to parse %s \"%s\"\n",
2728                                 c->log, c->argv[0], c->argv[1] );
2729                         return 1;
2730                 }
2731                 return(ldap_pvt_tls_set_option(NULL, flag, &i));
2732         } else {
2733                 return(ldap_int_tls_config(NULL, flag, c->argv[1]));
2734         }
2735 }
2736 #endif
2737
2738 static CfEntryInfo *
2739 config_find_base( CfEntryInfo *root, struct berval *dn, CfEntryInfo **last )
2740 {
2741         struct berval cdn;
2742         char *c;
2743
2744         if ( !root ) {
2745                 *last = NULL;
2746                 return NULL;
2747         }
2748
2749         if ( dn_match( &root->ce_entry->e_nname, dn ))
2750                 return root;
2751
2752         c = dn->bv_val+dn->bv_len;
2753         for (;*c != ',';c--);
2754
2755         while(root) {
2756                 *last = root;
2757                 for (--c;c>dn->bv_val && *c != ',';c--);
2758                 cdn.bv_val = c;
2759                 if ( *c == ',' )
2760                         cdn.bv_val++;
2761                 cdn.bv_len = dn->bv_len - (cdn.bv_val - dn->bv_val);
2762
2763                 root = root->ce_kids;
2764
2765                 for (;root;root=root->ce_sibs) {
2766                         if ( dn_match( &root->ce_entry->e_nname, &cdn )) {
2767                                 if ( cdn.bv_val == dn->bv_val ) {
2768                                         return root;
2769                                 }
2770                                 break;
2771                         }
2772                 }
2773         }
2774         return root;
2775 }
2776
2777 typedef struct setup_cookie {
2778         CfBackInfo *cfb;
2779         ConfigArgs *ca;
2780 } setup_cookie;
2781
2782 static int
2783 config_ldif_resp( Operation *op, SlapReply *rs )
2784 {
2785         if ( rs->sr_type == REP_SEARCH ) {
2786                 setup_cookie *sc = op->o_callback->sc_private;
2787
2788                 sc->cfb->cb_got_ldif = 1;
2789                 rs->sr_err = config_add_internal( sc->cfb, rs->sr_entry, sc->ca, NULL, NULL );
2790                 if ( rs->sr_err != LDAP_SUCCESS ) {
2791                         Debug( LDAP_DEBUG_ANY, "config error processing %s: %s\n",
2792                                 rs->sr_entry->e_name.bv_val, sc->ca->msg, 0 );
2793                 }
2794         }
2795         return rs->sr_err;
2796 }
2797
2798 /* Configure and read the underlying back-ldif store */
2799 static int
2800 config_setup_ldif( BackendDB *be, const char *dir, int readit ) {
2801         CfBackInfo *cfb = be->be_private;
2802         ConfigArgs c = {0};
2803         ConfigTable *ct;
2804         char *argv[3];
2805         int rc = 0;
2806         setup_cookie sc;
2807         slap_callback cb = { NULL, config_ldif_resp, NULL, NULL };
2808         Connection conn = {0};
2809         OperationBuffer opbuf;
2810         Operation *op;
2811         SlapReply rs = {REP_RESULT};
2812         Filter filter = { LDAP_FILTER_PRESENT };
2813         struct berval filterstr = BER_BVC("(objectclass=*)");
2814         struct stat st;
2815
2816         /* Is the config directory available? */
2817         if ( stat( dir, &st ) < 0 ) {
2818                 /* No, so don't bother using the backing store.
2819                  * All changes will be in-memory only.
2820                  */
2821                 return 0;
2822         }
2823                 
2824         cfb->cb_db.bd_info = backend_info( "ldif" );
2825         if ( !cfb->cb_db.bd_info )
2826                 return 0;       /* FIXME: eventually this will be a fatal error */
2827
2828         if ( backend_db_init( "ldif", &cfb->cb_db ) == NULL )
2829                 return 1;
2830
2831         cfb->cb_db.be_suffix = be->be_suffix;
2832         cfb->cb_db.be_nsuffix = be->be_nsuffix;
2833
2834         /* The suffix is always "cn=config". The underlying DB's rootdn
2835          * is always the same as the suffix.
2836          */
2837         cfb->cb_db.be_rootdn = be->be_suffix[0];
2838         cfb->cb_db.be_rootndn = be->be_nsuffix[0];
2839
2840         ber_str2bv( dir, 0, 1, &cfdir );
2841
2842         c.be = &cfb->cb_db;
2843         c.fname = "slapd";
2844         c.argc = 2;
2845         argv[0] = "directory";
2846         argv[1] = (char *)dir;
2847         argv[2] = NULL;
2848         c.argv = argv;
2849
2850         ct = config_find_keyword( c.be->be_cf_ocs->co_table, &c );
2851         if ( !ct )
2852                 return 1;
2853
2854         if ( config_add_vals( ct, &c ))
2855                 return 1;
2856
2857         if ( backend_startup_one( &cfb->cb_db ))
2858                 return 1;
2859
2860         if ( readit ) {
2861                 void *thrctx = ldap_pvt_thread_pool_context();
2862
2863                 op = (Operation *) &opbuf;
2864                 connection_fake_init( &conn, op, thrctx );
2865
2866                 filter.f_desc = slap_schema.si_ad_objectClass;
2867
2868                 op->o_tag = LDAP_REQ_SEARCH;
2869
2870                 op->ors_filter = &filter;
2871                 op->ors_filterstr = filterstr;
2872                 op->ors_scope = LDAP_SCOPE_SUBTREE;
2873
2874                 op->o_dn = c.be->be_rootdn;
2875                 op->o_ndn = c.be->be_rootndn;
2876
2877                 op->o_req_dn = be->be_suffix[0];
2878                 op->o_req_ndn = be->be_nsuffix[0];
2879
2880                 op->ors_tlimit = SLAP_NO_LIMIT;
2881                 op->ors_slimit = SLAP_NO_LIMIT;
2882
2883                 op->ors_attrs = slap_anlist_all_attributes;
2884                 op->ors_attrsonly = 0;
2885
2886                 op->o_callback = &cb;
2887                 sc.cfb = cfb;
2888                 sc.ca = &c;
2889                 cb.sc_private = &sc;
2890
2891                 op->o_bd = &cfb->cb_db;
2892                 rc = op->o_bd->be_search( op, &rs );
2893
2894                 ldap_pvt_thread_pool_context_reset( thrctx );
2895         }
2896
2897         /* ITS#4194 - only use if it's present, or we're converting. */
2898         if ( !readit || rc == LDAP_SUCCESS )
2899                 cfb->cb_use_ldif = 1;
2900
2901         return rc;
2902 }
2903
2904 static int
2905 CfOc_cmp( const void *c1, const void *c2 ) {
2906         const ConfigOCs *co1 = c1;
2907         const ConfigOCs *co2 = c2;
2908
2909         return ber_bvcmp( co1->co_name, co2->co_name );
2910 }
2911
2912 int
2913 config_register_schema(ConfigTable *ct, ConfigOCs *ocs) {
2914         int i;
2915
2916         i = init_config_attrs( ct );
2917         if ( i ) return i;
2918
2919         /* set up the objectclasses */
2920         i = init_config_ocs( ocs );
2921         if ( i ) return i;
2922
2923         for (i=0; ocs[i].co_def; i++) {
2924                 if ( ocs[i].co_oc ) {
2925                         ocs[i].co_name = &ocs[i].co_oc->soc_cname;
2926                         if ( !ocs[i].co_table )
2927                                 ocs[i].co_table = ct;
2928                         avl_insert( &CfOcTree, &ocs[i], CfOc_cmp, avl_dup_error );
2929                 }
2930         }
2931         return 0;
2932 }
2933
2934 int
2935 read_config(const char *fname, const char *dir) {
2936         BackendDB *be;
2937         CfBackInfo *cfb;
2938         const char *cfdir, *cfname;
2939         int rc;
2940
2941         /* Setup the config backend */
2942         be = backend_db_init( "config", NULL );
2943         if ( !be )
2944                 return 1;
2945
2946         cfb = be->be_private;
2947
2948         /* If no .conf, or a dir was specified, setup the dir */
2949         if ( !fname || dir ) {
2950                 if ( dir ) {
2951                         /* If explicitly given, check for existence */
2952                         struct stat st;
2953
2954                         if ( stat( dir, &st ) < 0 ) {
2955                                 Debug( LDAP_DEBUG_ANY,
2956                                         "invalid config directory %s, error %d\n",
2957                                                 dir, errno, 0 );
2958                                 return 1;
2959                         }
2960                         cfdir = dir;
2961                 } else {
2962                         cfdir = SLAPD_DEFAULT_CONFIGDIR;
2963                 }
2964                 /* if fname is defaulted, try reading .d */
2965                 rc = config_setup_ldif( be, cfdir, !fname );
2966
2967                 if ( rc ) {
2968                         /* It may be OK if the base object doesn't exist yet. */
2969                         if ( rc != LDAP_NO_SUCH_OBJECT )
2970                                 return 1;
2971                         /* ITS#4194: But if dir was specified and no fname,
2972                          * then we were supposed to read the dir.
2973                          */
2974                         if ( dir && !fname )
2975                                 return 1;
2976                 }
2977
2978                 /* If we read the config from back-ldif, nothing to do here */
2979                 if ( cfb->cb_got_ldif ) {
2980                         rc = 0;
2981                         goto done;
2982                 }
2983         }
2984
2985         if ( fname )
2986                 cfname = fname;
2987         else
2988                 cfname = SLAPD_DEFAULT_CONFIGFILE;
2989
2990         rc = read_config_file(cfname, 0, NULL, config_back_cf_table);
2991
2992         if ( rc == 0 )
2993                 ber_str2bv( cfname, 0, 1, &cfb->cb_config->c_file );
2994
2995         /* If we got this far and failed, it may be a serious problem. In server
2996          * mode, we should never come to this. However, it may be alright if we're
2997          * using slapadd to create the conf dir.
2998          */
2999         while ( rc ) {
3000                 if ( slapMode & (SLAP_SERVER_MODE|SLAP_TOOL_READMAIN|SLAP_TOOL_READONLY))
3001                         break;
3002                 /* If a config file was explicitly given, fail */
3003                 if ( fname )
3004                         break;
3005                 
3006                 /* Seems to be slapadd with a config dir, let it continue */
3007                 if ( cfb->cb_use_ldif ) {
3008                         rc = 0;
3009                         cfb->cb_got_ldif = 1;
3010                 }
3011                 break;
3012         }
3013
3014 done:
3015         if ( rc == 0 && BER_BVISNULL( &frontendDB->be_schemadn ) ) {
3016                 ber_str2bv( SLAPD_SCHEMA_DN, STRLENOF( SLAPD_SCHEMA_DN ), 1,
3017                         &frontendDB->be_schemadn );
3018                 rc = dnNormalize( 0, NULL, NULL, &frontendDB->be_schemadn, &frontendDB->be_schemandn, NULL );
3019                 if ( rc != LDAP_SUCCESS ) {
3020                         Debug(LDAP_DEBUG_ANY, "read_config: "
3021                                 "unable to normalize default schema DN \"%s\"\n",
3022                                 frontendDB->be_schemadn.bv_val, 0, 0 );
3023                         /* must not happen */
3024                         assert( 0 );
3025                 }
3026         }
3027         return rc;
3028 }
3029
3030 static int
3031 config_back_bind( Operation *op, SlapReply *rs )
3032 {
3033         if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op )) {
3034                 ber_dupbv( &op->orb_edn, be_root_dn( op->o_bd ));
3035                 /* frontend sends result */
3036                 return LDAP_SUCCESS;
3037         }
3038
3039         rs->sr_err = LDAP_INVALID_CREDENTIALS;
3040         send_ldap_result( op, rs );
3041
3042         return rs->sr_err;
3043 }
3044
3045 static int
3046 config_send( Operation *op, SlapReply *rs, CfEntryInfo *ce, int depth )
3047 {
3048         int rc = 0;
3049
3050         if ( test_filter( op, ce->ce_entry, op->ors_filter ) == LDAP_COMPARE_TRUE )
3051         {
3052                 rs->sr_attrs = op->ors_attrs;
3053                 rs->sr_entry = ce->ce_entry;
3054                 rs->sr_flags = 0;
3055                 rc = send_search_entry( op, rs );
3056         }
3057         if ( op->ors_scope == LDAP_SCOPE_SUBTREE ) {
3058                 if ( ce->ce_kids ) {
3059                         rc = config_send( op, rs, ce->ce_kids, 1 );
3060                         if ( rc ) return rc;
3061                 }
3062                 if ( depth ) {
3063                         for (ce=ce->ce_sibs; ce; ce=ce->ce_sibs) {
3064                                 rc = config_send( op, rs, ce, 0 );
3065                                 if ( rc ) break;
3066                         }
3067                 }
3068         }
3069         return rc;
3070 }
3071
3072 static ConfigTable *
3073 config_find_table( ConfigOCs **colst, int nocs, AttributeDescription *ad )
3074 {
3075         int i, j;
3076
3077         for (j=0; j<nocs; j++) {
3078                 for (i=0; colst[j]->co_table[i].name; i++)
3079                         if ( colst[j]->co_table[i].ad == ad )
3080                                 return &colst[j]->co_table[i];
3081         }
3082         return NULL;
3083 }
3084
3085 /* Sort the attributes of the entry according to the order defined
3086  * in the objectclass, with required attributes occurring before
3087  * allowed attributes. For any attributes with sequencing dependencies
3088  * (e.g., rootDN must be defined after suffix) the objectclass must
3089  * list the attributes in the desired sequence.
3090  */
3091 static void
3092 sort_attrs( Entry *e, ConfigOCs **colst, int nocs )
3093 {
3094         Attribute *a, *head = NULL, *tail = NULL, **prev;
3095         int i, j;
3096
3097         for (i=0; i<nocs; i++) {
3098                 if ( colst[i]->co_oc->soc_required ) {
3099                         AttributeType **at = colst[i]->co_oc->soc_required;
3100                         for (j=0; at[j]; j++) {
3101                                 for (a=e->e_attrs, prev=&e->e_attrs; a;
3102                                         prev = &(*prev)->a_next, a=a->a_next) {
3103                                         if ( a->a_desc == at[j]->sat_ad ) {
3104                                                 *prev = a->a_next;
3105                                                 if (!head) {
3106                                                         head = a;
3107                                                         tail = a;
3108                                                 } else {
3109                                                         tail->a_next = a;
3110                                                         tail = a;
3111                                                 }
3112                                                 break;
3113                                         }
3114                                 }
3115                         }
3116                 }
3117                 if ( colst[i]->co_oc->soc_allowed ) {
3118                         AttributeType **at = colst[i]->co_oc->soc_allowed;
3119                         for (j=0; at[j]; j++) {
3120                                 for (a=e->e_attrs, prev=&e->e_attrs; a;
3121                                         prev = &(*prev)->a_next, a=a->a_next) {
3122                                         if ( a->a_desc == at[j]->sat_ad ) {
3123                                                 *prev = a->a_next;
3124                                                 if (!head) {
3125                                                         head = a;
3126                                                         tail = a;
3127                                                 } else {
3128                                                         tail->a_next = a;
3129                                                         tail = a;
3130                                                 }
3131                                                 break;
3132                                         }
3133                                 }
3134                         }
3135                 }
3136         }
3137         if ( tail ) {
3138                 tail->a_next = e->e_attrs;
3139                 e->e_attrs = head;
3140         }
3141 }
3142
3143 static int
3144 check_vals( ConfigTable *ct, ConfigArgs *ca, void *ptr, int isAttr )
3145 {
3146         Attribute *a = NULL;
3147         AttributeDescription *ad;
3148         BerVarray vals;
3149
3150         int i, rc = 0, sort = 0;
3151
3152         if ( isAttr ) {
3153                 a = ptr;
3154                 ad = a->a_desc;
3155                 vals = a->a_vals;
3156         } else {
3157                 Modifications *ml = ptr;
3158                 ad = ml->sml_desc;
3159                 vals = ml->sml_values;
3160         }
3161
3162         if ( a && ( ad->ad_type->sat_flags & SLAP_AT_ORDERED_VAL )) {
3163                 sort = 1;
3164                 rc = ordered_value_sort( a, 1 );
3165                 if ( rc ) {
3166                         snprintf(ca->msg, sizeof( ca->msg ), "ordered_value_sort failed on attr %s\n",
3167                                 ad->ad_cname.bv_val );
3168                         return rc;
3169                 }
3170         }
3171         for ( i=0; vals[i].bv_val; i++ ) {
3172                 ca->line = vals[i].bv_val;
3173                 if ( sort ) {
3174                         char *idx = strchr( ca->line, '}' );
3175                         if ( idx ) ca->line = idx+1;
3176                 }
3177                 rc = config_parse_vals( ct, ca, i );
3178                 if ( rc ) {
3179                         break;
3180                 }
3181         }
3182         return rc;
3183 }
3184
3185 static int
3186 check_name_index( CfEntryInfo *parent, ConfigType ce_type, Entry *e,
3187         SlapReply *rs, int *renum )
3188 {
3189         CfEntryInfo *ce;
3190         int index = -1, gotindex = 0, nsibs;
3191         int renumber = 0, tailindex = 0;
3192         char *ptr1, *ptr2 = NULL;
3193         struct berval rdn;
3194
3195         if ( renum ) *renum = 0;
3196
3197         /* These entries don't get indexed/renumbered */
3198         if ( ce_type == Cft_Global ) return 0;
3199         if ( ce_type == Cft_Schema && parent->ce_type == Cft_Global ) return 0;
3200
3201         if ( ce_type == Cft_Include || ce_type == Cft_Module )
3202                 tailindex = 1;
3203
3204         /* See if the rdn has an index already */
3205         dnRdn( &e->e_name, &rdn );
3206         ptr1 = ber_bvchr( &e->e_name, '{' );
3207         if ( ptr1 && ptr1 - e->e_name.bv_val < rdn.bv_len ) {
3208                 char    *next;
3209                 ptr2 = strchr( ptr1, '}' );
3210                 if (!ptr2 || ptr2 - e->e_name.bv_val > rdn.bv_len)
3211                         return LDAP_NAMING_VIOLATION;
3212                 if ( ptr2-ptr1 == 1)
3213                         return LDAP_NAMING_VIOLATION;
3214                 gotindex = 1;
3215                 index = strtol( ptr1 + 1, &next, 10 );
3216                 if ( next == ptr1 + 1 || next[ 0 ] != '}' ) {
3217                         return LDAP_NAMING_VIOLATION;
3218                 }
3219                 if ( index < 0 ) {
3220                         /* Special case, we allow -1 for the frontendDB */
3221                         if ( index != -1 || ce_type != Cft_Database ||
3222                                 strncmp( ptr2+1, "frontend,", STRLENOF("frontend,") ))
3223
3224                                 return LDAP_NAMING_VIOLATION;
3225                 }
3226         }
3227
3228         /* count related kids */
3229         for (nsibs=0, ce=parent->ce_kids; ce; ce=ce->ce_sibs) {
3230                 if ( ce->ce_type == ce_type ) nsibs++;
3231         }
3232
3233         if ( index != nsibs ) {
3234                 if ( gotindex ) {
3235                         if ( index < nsibs ) {
3236                                 if ( tailindex ) return LDAP_NAMING_VIOLATION;
3237                                 /* Siblings need to be renumbered */
3238                                 renumber = 1;
3239                         }
3240                 }
3241                 if ( !renumber ) {
3242                         struct berval ival, newrdn, nnewrdn;
3243                         struct berval rtype, rval;
3244                         Attribute *a;
3245                         AttributeDescription *ad = NULL;
3246                         char ibuf[32];
3247                         const char *text;
3248
3249                         rval.bv_val = strchr(rdn.bv_val, '=' ) + 1;
3250                         rval.bv_len = rdn.bv_len - (rval.bv_val - rdn.bv_val);
3251                         rtype.bv_val = rdn.bv_val;
3252                         rtype.bv_len = rval.bv_val - rtype.bv_val - 1;
3253
3254                         /* Find attr */
3255                         slap_bv2ad( &rtype, &ad, &text );
3256                         a = attr_find( e->e_attrs, ad );
3257                         if (!a ) return LDAP_NAMING_VIOLATION;
3258
3259                         ival.bv_val = ibuf;
3260                         ival.bv_len = snprintf( ibuf, sizeof( ibuf ), SLAP_X_ORDERED_FMT, nsibs );
3261                         if ( ival.bv_len >= sizeof( ibuf ) ) {
3262                                 return LDAP_NAMING_VIOLATION;
3263                         }
3264                         
3265                         newrdn.bv_len = rdn.bv_len + ival.bv_len;
3266                         newrdn.bv_val = ch_malloc( newrdn.bv_len+1 );
3267
3268                         if ( tailindex ) {
3269                                 ptr1 = lutil_strncopy( newrdn.bv_val, rdn.bv_val, rdn.bv_len );
3270                                 ptr1 = lutil_strcopy( ptr1, ival.bv_val );
3271                         } else {
3272                                 int xlen;
3273                                 if ( !gotindex ) {
3274                                         ptr2 = rval.bv_val;
3275                                         xlen = rval.bv_len;
3276                                 } else {
3277                                         xlen = rdn.bv_len - (ptr2 - rdn.bv_val);
3278                                 }
3279                                 ptr1 = lutil_strncopy( newrdn.bv_val, rtype.bv_val,
3280                                         rtype.bv_len );
3281                                 *ptr1++ = '=';
3282                                 ptr1 = lutil_strcopy( ptr1, ival.bv_val );
3283                                 ptr1 = lutil_strncopy( ptr1, ptr2, xlen );
3284                                 *ptr1 = '\0';
3285                         }
3286
3287                         /* Do the equivalent of ModRDN */
3288                         /* Replace DN / NDN */
3289                         newrdn.bv_len = ptr1 - newrdn.bv_val;
3290                         rdnNormalize( 0, NULL, NULL, &newrdn, &nnewrdn, NULL );
3291                         free( e->e_name.bv_val );
3292                         build_new_dn( &e->e_name, &parent->ce_entry->e_name,
3293                                 &newrdn, NULL );
3294                         free( e->e_nname.bv_val );
3295                         build_new_dn( &e->e_nname, &parent->ce_entry->e_nname,
3296                                 &nnewrdn, NULL );
3297
3298                         /* Replace attr */
3299                         free( a->a_vals[0].bv_val );
3300                         ptr1 = strchr( newrdn.bv_val, '=' ) + 1;
3301                         a->a_vals[0].bv_len = newrdn.bv_len - (ptr1 - newrdn.bv_val);
3302                         a->a_vals[0].bv_val = ch_malloc( a->a_vals[0].bv_len + 1 );
3303                         strcpy( a->a_vals[0].bv_val, ptr1 );
3304
3305                         if ( a->a_nvals != a->a_vals ) {
3306                                 free( a->a_nvals[0].bv_val );
3307                                 ptr1 = strchr( nnewrdn.bv_val, '=' ) + 1;
3308                                 a->a_nvals[0].bv_len = nnewrdn.bv_len - (ptr1 - nnewrdn.bv_val);
3309                                 a->a_nvals[0].bv_val = ch_malloc( a->a_nvals[0].bv_len + 1 );
3310                                 strcpy( a->a_nvals[0].bv_val, ptr1 );
3311                         }
3312                         free( nnewrdn.bv_val );
3313                         free( newrdn.bv_val );
3314                 }
3315         }
3316         if ( renum ) *renum = renumber;
3317         return 0;
3318 }
3319
3320 static ConfigOCs **
3321 count_ocs( Attribute *oc_at, int *nocs )
3322 {
3323         int i, j, n;
3324         ConfigOCs co, *coptr, **colst;
3325
3326         /* count the objectclasses */
3327         for ( i=0; oc_at->a_nvals[i].bv_val; i++ );
3328         n = i;
3329         colst = (ConfigOCs **)ch_malloc( n * sizeof(ConfigOCs *));
3330
3331         for ( i=0, j=0; i<n; i++) {
3332                 co.co_name = &oc_at->a_nvals[i];
3333                 coptr = avl_find( CfOcTree, &co, CfOc_cmp );
3334                 
3335                 /* ignore non-config objectclasses. probably should be
3336                  * an error, general data doesn't belong here.
3337                  */
3338                 if ( !coptr ) continue;
3339
3340                 /* Ignore the root objectclass, it has no implementation.
3341                  */
3342                 if ( coptr->co_type == Cft_Abstract ) continue;
3343                 colst[j++] = coptr;
3344         }
3345         *nocs = j;
3346         return colst;
3347 }
3348
3349 static int
3350 cfAddInclude( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
3351 {
3352         if ( p->ce_type != Cft_Global && p->ce_type != Cft_Include )
3353                 return LDAP_CONSTRAINT_VIOLATION;
3354
3355         /* If we're reading from a configdir, don't parse this entry */
3356         if ( ca->lineno )
3357                 return LDAP_COMPARE_TRUE;
3358
3359         cfn = p->ce_private;
3360         ca->private = cfn;
3361         return LDAP_SUCCESS;
3362 }
3363
3364 static int
3365 cfAddSchema( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
3366 {
3367         ConfigFile *cfo;
3368
3369         /* This entry is hardcoded, don't re-parse it */
3370         if ( p->ce_type == Cft_Global ) {
3371                 cfn = p->ce_private;
3372                 ca->private = cfn;
3373                 return LDAP_COMPARE_TRUE;
3374         }
3375         if ( p->ce_type != Cft_Schema )
3376                 return LDAP_CONSTRAINT_VIOLATION;
3377
3378         cfn = ch_calloc( 1, sizeof(ConfigFile) );
3379         ca->private = cfn;
3380         cfo = p->ce_private;
3381         cfn->c_sibs = cfo->c_kids;
3382         cfo->c_kids = cfn;
3383         return LDAP_SUCCESS;
3384 }
3385
3386 static int
3387 cfAddDatabase( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
3388 {
3389         if ( p->ce_type != Cft_Global )
3390                 return LDAP_CONSTRAINT_VIOLATION;
3391         ca->be = frontendDB;    /* just to get past check_vals */
3392         return LDAP_SUCCESS;
3393 }
3394
3395 static int
3396 cfAddBackend( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
3397 {
3398         if ( p->ce_type != Cft_Global )
3399                 return LDAP_CONSTRAINT_VIOLATION;
3400         return LDAP_SUCCESS;
3401 }
3402
3403 static int
3404 cfAddModule( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
3405 {
3406         if ( p->ce_type != Cft_Global )
3407                 return LDAP_CONSTRAINT_VIOLATION;
3408         return LDAP_SUCCESS;
3409 }
3410
3411 static int
3412 cfAddOverlay( CfEntryInfo *p, Entry *e, struct config_args_s *ca )
3413 {
3414         if ( p->ce_type != Cft_Database )
3415                 return LDAP_CONSTRAINT_VIOLATION;
3416         ca->be = p->ce_be;
3417         return LDAP_SUCCESS;
3418 }
3419
3420 /* Parse an LDAP entry into config directives */
3421 static int
3422 config_add_internal( CfBackInfo *cfb, Entry *e, ConfigArgs *ca, SlapReply *rs, int *renum )
3423 {
3424         CfEntryInfo *ce, *last;
3425         ConfigOCs **colst;
3426         Attribute *a, *oc_at;
3427         int i, nocs, rc = 0;
3428         struct berval pdn;
3429         ConfigTable *ct;
3430         char *ptr;
3431
3432         /* Make sure parent exists and entry does not */
3433         ce = config_find_base( cfb->cb_root, &e->e_nname, &last );
3434         if ( ce )
3435                 return LDAP_ALREADY_EXISTS;
3436
3437         dnParent( &e->e_nname, &pdn );
3438
3439         /* If last is NULL, the new entry is the root/suffix entry, 
3440          * otherwise last should be the parent.
3441          */
3442         if ( last && !dn_match( &last->ce_entry->e_nname, &pdn )) {
3443                 if ( rs )
3444                         rs->sr_matched = last->ce_entry->e_name.bv_val;
3445                 return LDAP_NO_SUCH_OBJECT;
3446         }
3447
3448         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
3449         if ( !oc_at ) return LDAP_OBJECT_CLASS_VIOLATION;
3450
3451         memset( ca, 0, sizeof(ConfigArgs));
3452
3453         /* Fake the coordinates based on whether we're part of an
3454          * LDAP Add or if reading the config dir
3455          */
3456         if ( rs ) {
3457                 ca->fname = "slapd";
3458                 ca->lineno = 0;
3459         } else {
3460                 ca->fname = cfdir.bv_val;
3461                 ca->lineno = 1;
3462         }
3463
3464         colst = count_ocs( oc_at, &nocs );
3465
3466         /* Only the root can be Cft_Global, everything else must
3467          * have a parent. Only limited nesting arrangements are allowed.
3468          */
3469         rc = LDAP_CONSTRAINT_VIOLATION;
3470         if ( colst[0]->co_type == Cft_Global && !last ) {
3471                 cfn = cfb->cb_config;
3472                 ca->private = cfn;
3473                 ca->be = frontendDB;    /* just to get past check_vals */
3474                 rc = LDAP_SUCCESS;
3475         }
3476
3477         /* Check whether the Add is allowed by its parent, and do
3478          * any necessary arg setup
3479          */
3480         if ( last ) {
3481                 for ( i=0; i<nocs; i++ ) {
3482                         if ( colst[i]->co_ldadd &&
3483                                 ( rc = colst[i]->co_ldadd( last, e, ca ))
3484                                         != LDAP_CONSTRAINT_VIOLATION ) {
3485                                 break;
3486                         }
3487                 }
3488         }
3489
3490         /* Add the entry but don't parse it, we already have its contents */
3491         if ( rc == LDAP_COMPARE_TRUE ) {
3492                 rc = LDAP_SUCCESS;
3493                 goto ok;
3494         }
3495
3496         if ( rc != LDAP_SUCCESS )
3497                 goto done;
3498
3499         /* Parse all the values and check for simple syntax errors before
3500          * performing any set actions.
3501          *
3502          * If doing an LDAPadd, check for indexed names and any necessary
3503          * renaming/renumbering. Entries that don't need indexed names are
3504          * ignored. Entries that need an indexed name and arrive without one
3505          * are assigned to the end. Entries that arrive with an index may
3506          * cause the following entries to be renumbered/bumped down.
3507          *
3508          * Note that "pseudo-indexed" entries (cn=Include{xx}, cn=Module{xx})
3509          * don't allow Adding an entry with an index that's already in use.
3510          * This is flagged as an error (LDAP_ALREADY_EXISTS) up above.
3511          *
3512          * These entries can have auto-assigned indexes (appended to the end)
3513          * but only the other types support auto-renumbering of siblings.
3514          */
3515         rc = check_name_index( last, colst[0]->co_type, e, rs, renum );
3516         if ( rc )
3517                 goto done;
3518
3519         init_config_argv( ca );
3520
3521         /* Make sure we process attrs in the required order */
3522         sort_attrs( e, colst, nocs );
3523
3524         for ( a=e->e_attrs; a; a=a->a_next ) {
3525                 if ( a == oc_at ) continue;
3526                 ct = config_find_table( colst, nocs, a->a_desc );
3527                 if ( !ct ) continue;    /* user data? */
3528                 rc = check_vals( ct, ca, a, 1 );
3529                 if ( rc ) goto done;
3530         }
3531
3532         /* Basic syntax checks are OK. Do the actual settings. */
3533         for ( a=e->e_attrs; a; a=a->a_next ) {
3534                 if ( a == oc_at ) continue;
3535                 ct = config_find_table( colst, nocs, a->a_desc );
3536                 if ( !ct ) continue;    /* user data? */
3537                 for (i=0; a->a_vals[i].bv_val; i++) {
3538                         ca->line = a->a_vals[i].bv_val;
3539                         if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED ) {
3540                                 ptr = strchr( ca->line, '}' );
3541                                 if ( ptr ) ca->line = ptr+1;
3542                         }
3543                         ca->valx = i;
3544                         rc = config_parse_add( ct, ca );
3545                         if ( rc ) {
3546                                 rc = LDAP_OTHER;
3547                                 goto done;
3548                         }
3549                 }
3550         }
3551 ok:
3552         /* Newly added databases and overlays need to be started up */
3553         if ( CONFIG_ONLINE_ADD( ca )) {
3554                 if ( colst[0]->co_type == Cft_Database ) {
3555                         rc = backend_startup_one( ca->be );
3556
3557                 } else if ( colst[0]->co_type == Cft_Overlay ) {
3558                         if ( ca->bi->bi_db_open ) {
3559                                 BackendInfo *bi_orig = ca->be->bd_info;
3560                                 ca->be->bd_info = ca->bi;
3561                                 rc = ca->bi->bi_db_open( ca->be );
3562                                 ca->be->bd_info = bi_orig;
3563                         }
3564                 }
3565                 if ( rc ) {
3566                         snprintf( ca->msg, sizeof( ca->msg ), "<%s> failed startup", ca->argv[0] );
3567                         Debug(LDAP_DEBUG_ANY, "%s: %s (%s)!\n",
3568                                 ca->log, ca->msg, ca->argv[1] );
3569                         rc = LDAP_OTHER;
3570                         goto done;
3571                 }
3572         }
3573
3574         ce = ch_calloc( 1, sizeof(CfEntryInfo) );
3575         ce->ce_parent = last;
3576         ce->ce_entry = entry_dup( e );
3577         ce->ce_entry->e_private = ce;
3578         ce->ce_type = colst[0]->co_type;
3579         ce->ce_be = ca->be;
3580         ce->ce_bi = ca->bi;
3581         ce->ce_private = ca->private;
3582         if ( !last ) {
3583                 cfb->cb_root = ce;
3584         } else if ( last->ce_kids ) {
3585                 CfEntryInfo *c2;
3586
3587                 for (c2=last->ce_kids; c2 && c2->ce_sibs; c2 = c2->ce_sibs);
3588
3589                 c2->ce_sibs = ce;
3590         } else {
3591                 last->ce_kids = ce;
3592         }
3593
3594 done:
3595         if ( rc ) {
3596                 if ( (colst[0]->co_type == Cft_Database) && ca->be ) {
3597                         if ( ca->be != frontendDB )
3598                                 backend_destroy_one( ca->be, 1 );
3599                 } else if ( (colst[0]->co_type == Cft_Overlay) && ca->bi ) {
3600                         overlay_destroy_one( ca->be, (slap_overinst *)ca->bi );
3601                 }
3602         }
3603
3604         ch_free( ca->argv );
3605         if ( colst ) ch_free( colst );
3606         return rc;
3607 }
3608
3609 /* Parse an LDAP entry into config directives, then store in underlying
3610  * database.
3611  */
3612 static int
3613 config_back_add( Operation *op, SlapReply *rs )
3614 {
3615         CfBackInfo *cfb;
3616         int renumber;
3617         ConfigArgs ca;
3618
3619         if ( !be_isroot( op ) ) {
3620                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
3621                 goto out;
3622         }
3623
3624         cfb = (CfBackInfo *)op->o_bd->be_private;
3625
3626         ldap_pvt_thread_pool_pause( &connection_pool );
3627
3628         /* Strategy:
3629          * 1) check for existence of entry
3630          * 2) check for sibling renumbering
3631          * 3) perform internal add
3632          * 4) store entry in underlying database
3633          * 5) perform any necessary renumbering
3634          */
3635         rs->sr_err = config_add_internal( cfb, op->ora_e, &ca, rs, &renumber );
3636         if ( rs->sr_err != LDAP_SUCCESS ) {
3637                 rs->sr_text = ca.msg;
3638         } else if ( cfb->cb_use_ldif ) {
3639                 BackendDB *be = op->o_bd;
3640                 slap_callback sc = { NULL, slap_null_cb, NULL, NULL };
3641                 struct berval dn, ndn;
3642
3643                 op->o_bd = &cfb->cb_db;
3644
3645                 /* Save current rootdn; use the underlying DB's rootdn */
3646                 dn = op->o_dn;
3647                 ndn = op->o_ndn;
3648                 op->o_dn = op->o_bd->be_rootdn;
3649                 op->o_ndn = op->o_bd->be_rootndn;
3650
3651                 sc.sc_next = op->o_callback;
3652                 op->o_callback = &sc;
3653                 op->o_bd->be_add( op, rs );
3654                 op->o_bd = be;
3655                 op->o_callback = sc.sc_next;
3656                 op->o_dn = dn;
3657                 op->o_ndn = ndn;
3658         }
3659         if ( renumber ) {
3660         }
3661
3662         ldap_pvt_thread_pool_resume( &connection_pool );
3663
3664 out:
3665         send_ldap_result( op, rs );
3666         return rs->sr_err;
3667 }
3668
3669 typedef struct delrec {
3670         struct delrec *next;
3671         int nidx;
3672         int idx[1];
3673 } delrec;
3674
3675 static int
3676 config_modify_internal( CfEntryInfo *ce, Operation *op, SlapReply *rs,
3677         ConfigArgs *ca )
3678 {
3679         int rc = LDAP_UNWILLING_TO_PERFORM;
3680         Modifications *ml;
3681         Entry *e = ce->ce_entry;
3682         Attribute *save_attrs = e->e_attrs, *oc_at;
3683         ConfigTable *ct;
3684         ConfigOCs **colst;
3685         int i, nocs;
3686         char *ptr;
3687         delrec *dels = NULL, *deltail = NULL;
3688
3689         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
3690         if ( !oc_at ) return LDAP_OBJECT_CLASS_VIOLATION;
3691
3692         colst = count_ocs( oc_at, &nocs );
3693
3694         e->e_attrs = attrs_dup( e->e_attrs );
3695
3696         init_config_argv( ca );
3697         ca->be = ce->ce_be;
3698         ca->bi = ce->ce_bi;
3699         ca->private = ce->ce_private;
3700         ca->ca_entry = e;
3701         strcpy( ca->log, "back-config" );
3702
3703         for (ml = op->orm_modlist; ml; ml=ml->sml_next) {
3704                 ct = config_find_table( colst, nocs, ml->sml_desc );
3705                 switch (ml->sml_op) {
3706                 case LDAP_MOD_DELETE:
3707                 case LDAP_MOD_REPLACE: {
3708                         BerVarray vals = NULL, nvals = NULL;
3709                         int *idx = NULL;
3710                         if ( ct && ( ct->arg_type & ARG_NO_DELETE )) {
3711                                 rc = LDAP_OTHER;
3712                                 snprintf(ca->msg, sizeof(ca->msg), "cannot delete %s",
3713                                         ml->sml_desc->ad_cname.bv_val );
3714                                 goto out;
3715                         }
3716                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
3717                                 vals = ml->sml_values;
3718                                 nvals = ml->sml_nvalues;
3719                                 ml->sml_values = NULL;
3720                                 ml->sml_nvalues = NULL;
3721                         }
3722                         /* If we're deleting by values, remember the indexes of the
3723                          * values we deleted.
3724                          */
3725                         if ( ct && ml->sml_values ) {
3726                                 delrec *d;
3727                                 for (i=0; ml->sml_values[i].bv_val; i++);
3728                                 d = ch_malloc( sizeof(delrec) + (i - 1)* sizeof(int));
3729                                 d->nidx = i;
3730                                 d->next = NULL;
3731                                 if ( dels ) {
3732                                         deltail->next = d;
3733                                 } else {
3734                                         dels = d;
3735                                 }
3736                                 deltail = d;
3737                                 idx = d->idx;
3738                         }
3739                         rc = modify_delete_vindex(e, &ml->sml_mod,
3740                                 get_permissiveModify(op),
3741                                 &rs->sr_text, ca->msg, sizeof(ca->msg), idx );
3742                         if ( ml->sml_op == LDAP_MOD_REPLACE ) {
3743                                 ml->sml_values = vals;
3744                                 ml->sml_nvalues = nvals;
3745                         }
3746                         if ( !vals )
3747                                 break;
3748                         }
3749                         /* FALLTHRU: LDAP_MOD_REPLACE && vals */
3750
3751                 case LDAP_MOD_ADD:
3752                 case SLAP_MOD_SOFTADD: {
3753                         int mop = ml->sml_op;
3754                         int navals = -1;
3755                         ml->sml_op = LDAP_MOD_ADD;
3756                         if ( ct ) {
3757                                 if ( ct->arg_type & ARG_NO_INSERT ) {
3758                                         Attribute *a = attr_find( e->e_attrs, ml->sml_desc );
3759                                         if ( a ) {
3760                                                 for (i = 0; a->a_vals[i].bv_val; i++ );
3761                                                 navals = i;
3762                                         }
3763                                 }
3764                                 for ( i=0; !BER_BVISNULL( &ml->sml_values[i] ); i++ ) {
3765                                         if ( ml->sml_values[i].bv_val[0] == '{' &&
3766                                                 navals >= 0 )
3767                                         {
3768                                                 char    *next, *val = ml->sml_values[i].bv_val + 1;
3769                                                 int     j;
3770
3771                                                 j = strtol( val, &next, 0 );
3772                                                 if ( next == val || next[ 0 ] != '}' || j < navals ) {
3773                                                         rc = LDAP_OTHER;
3774                                                         snprintf(ca->msg, sizeof(ca->msg), "cannot insert %s",
3775                                                                 ml->sml_desc->ad_cname.bv_val );
3776                                                         goto out;
3777                                                 }
3778                                         }
3779                                         rc = check_vals( ct, ca, ml, 0 );
3780                                         if ( rc ) goto out;
3781                                 }
3782                         }
3783                         rc = modify_add_values(e, &ml->sml_mod,
3784                                    get_permissiveModify(op),
3785                                    &rs->sr_text, ca->msg, sizeof(ca->msg) );
3786
3787                         /* If value already exists, show success here
3788                          * and ignore this operation down below.
3789                          */
3790                         if ( mop == SLAP_MOD_SOFTADD ) {
3791                                 if ( rc == LDAP_TYPE_OR_VALUE_EXISTS )
3792                                         rc = LDAP_SUCCESS;
3793                                 else
3794                                         mop = LDAP_MOD_ADD;
3795                         }
3796                         ml->sml_op = mop;
3797                         break;
3798                         }
3799
3800                         break;
3801                 case LDAP_MOD_INCREMENT:        /* FIXME */
3802                         break;
3803                 default:
3804                         break;
3805                 }
3806                 if(rc != LDAP_SUCCESS) break;
3807         }
3808         
3809         if(rc == LDAP_SUCCESS) {
3810                 /* check that the entry still obeys the schema */
3811                 rc = entry_schema_check(op, e, NULL, 0,
3812                         &rs->sr_text, ca->msg, sizeof(ca->msg) );
3813         }
3814         if ( rc == LDAP_SUCCESS ) {
3815                 /* Basic syntax checks are OK. Do the actual settings. */
3816                 for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
3817                         ct = config_find_table( colst, nocs, ml->sml_desc );
3818                         if ( !ct ) continue;
3819
3820                         switch (ml->sml_op) {
3821                         case LDAP_MOD_DELETE:
3822                         case LDAP_MOD_REPLACE: {
3823                                 BerVarray vals = NULL, nvals = NULL;
3824                                 Attribute *a;
3825                                 delrec *d = NULL;
3826
3827                                 a = attr_find( e->e_attrs, ml->sml_desc );
3828
3829                                 if ( ml->sml_op == LDAP_MOD_REPLACE ) {
3830                                         vals = ml->sml_values;
3831                                         nvals = ml->sml_nvalues;
3832                                         ml->sml_values = NULL;
3833                                         ml->sml_nvalues = NULL;
3834                                 }
3835
3836                                 if ( ml->sml_values )
3837                                         d = dels;
3838
3839                                 /* If we didn't delete the whole attribute */
3840                                 if ( ml->sml_values && a ) {
3841                                         struct berval *mvals;
3842                                         int j;
3843
3844                                         if ( ml->sml_nvalues )
3845                                                 mvals = ml->sml_nvalues;
3846                                         else
3847                                                 mvals = ml->sml_values;
3848
3849                                         /* use the indexes we saved up above */
3850                                         for (i=0; i < d->nidx; i++) {
3851                                                 struct berval bv = *mvals++;
3852                                                 if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED &&
3853                                                         bv.bv_val[0] == '{' ) {
3854                                                         ptr = strchr( bv.bv_val, '}' ) + 1;
3855                                                         bv.bv_len -= ptr - bv.bv_val;
3856                                                         bv.bv_val = ptr;
3857                                                 }
3858                                                 ca->line = bv.bv_val;
3859                                                 ca->valx = d->idx[i];
3860                                                 rc = config_del_vals( ct, ca );
3861                                                 if ( rc != LDAP_SUCCESS ) break;
3862                                                 for (j=i+1; j < d->nidx; j++)
3863                                                         if ( d->idx[j] >d->idx[i] )
3864                                                                 d->idx[j]--;
3865                                         }
3866                                 } else {
3867                                         ca->valx = -1;
3868                                         ca->line = NULL;
3869                                         rc = config_del_vals( ct, ca );
3870                                         if ( rc ) rc = LDAP_OTHER;
3871                                 }
3872                                 if ( ml->sml_values ) {
3873                                         ch_free( dels );
3874                                         dels = d->next;
3875                                 }
3876                                 if ( ml->sml_op == LDAP_MOD_REPLACE ) {
3877                                         ml->sml_values = vals;
3878                                         ml->sml_nvalues = nvals;
3879                                 }
3880                                 if ( !vals || rc != LDAP_SUCCESS )
3881                                         break;
3882                                 }
3883                                 /* FALLTHRU: LDAP_MOD_REPLACE && vals */
3884
3885                         case LDAP_MOD_ADD:
3886                                 for (i=0; ml->sml_values[i].bv_val; i++) {
3887                                         ca->line = ml->sml_values[i].bv_val;
3888                                         ca->valx = -1;
3889                                         if ( ml->sml_desc->ad_type->sat_flags & SLAP_AT_ORDERED &&
3890                                                 ca->line[0] == '{' )
3891                                         {
3892                                                 ptr = strchr( ca->line + 1, '}' );
3893                                                 if ( ptr ) {
3894                                                         char    *next;
3895
3896                                                         ca->valx = strtol( ca->line + 1, &next, 0 );
3897                                                         if ( next == ca->line + 1 || next[ 0 ] != '}' ) {
3898                                                                 rc = LDAP_OTHER;
3899                                                                 goto out;
3900                                                         }
3901                                                         ca->line = ptr+1;
3902                                                 }
3903                                         }
3904                                         rc = config_parse_add( ct, ca );
3905                                         if ( rc ) {
3906                                                 rc = LDAP_OTHER;
3907                                                 goto out;
3908                                         }
3909                                 }
3910
3911                                 break;
3912                         }
3913                 }
3914         }
3915
3916 out:
3917         if ( ca->cleanup )
3918                 ca->cleanup( ca );
3919         if ( rc == LDAP_SUCCESS ) {
3920                 attrs_free( save_attrs );
3921         } else {
3922                 attrs_free( e->e_attrs );
3923                 e->e_attrs = save_attrs;
3924         }
3925         ch_free( ca->argv );
3926         if ( colst ) ch_free( colst );
3927
3928         return rc;
3929 }
3930
3931 static int
3932 config_back_modify( Operation *op, SlapReply *rs )
3933 {
3934         CfBackInfo *cfb;
3935         CfEntryInfo *ce, *last;
3936         Modifications *ml;
3937         ConfigArgs ca = {0};
3938         struct berval rdn;
3939         char *ptr;
3940         AttributeDescription *rad = NULL;
3941
3942         if ( !be_isroot( op ) ) {
3943                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
3944                 goto out;
3945         }
3946
3947         cfb = (CfBackInfo *)op->o_bd->be_private;
3948
3949         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
3950         if ( !ce ) {
3951                 if ( last )
3952                         rs->sr_matched = last->ce_entry->e_name.bv_val;
3953                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
3954                 goto out;
3955         }
3956
3957         /* Get type of RDN */
3958         rdn = ce->ce_entry->e_nname;
3959         ptr = strchr( rdn.bv_val, '=' );
3960         rdn.bv_len = ptr - rdn.bv_val;
3961         slap_bv2ad( &rdn, &rad, &rs->sr_text );
3962
3963         /* Some basic validation... */
3964         for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
3965                 /* Don't allow Modify of RDN; must use ModRdn for that. */
3966                 if ( ml->sml_desc == rad ) {
3967                         rs->sr_err = LDAP_NOT_ALLOWED_ON_RDN;
3968                         rs->sr_text = "Use modrdn to change the entry name";
3969                         goto out;
3970                 }
3971         }
3972
3973         ldap_pvt_thread_pool_pause( &connection_pool );
3974
3975         /* Strategy:
3976          * 1) perform the Modify on the cached Entry.
3977          * 2) verify that the Entry still satisfies the schema.
3978          * 3) perform the individual config operations.
3979          * 4) store Modified entry in underlying LDIF backend.
3980          */
3981         rs->sr_err = config_modify_internal( ce, op, rs, &ca );
3982         if ( rs->sr_err ) {
3983                 rs->sr_text = ca.msg;
3984         } else if ( cfb->cb_use_ldif ) {
3985                 BackendDB *be = op->o_bd;
3986                 slap_callback sc = { NULL, slap_null_cb, NULL, NULL };
3987                 struct berval dn, ndn;
3988
3989                 op->o_bd = &cfb->cb_db;
3990
3991                 dn = op->o_dn;
3992                 ndn = op->o_ndn;
3993                 op->o_dn = op->o_bd->be_rootdn;
3994                 op->o_ndn = op->o_bd->be_rootndn;
3995
3996                 sc.sc_next = op->o_callback;
3997                 op->o_callback = &sc;
3998                 op->o_bd->be_modify( op, rs );
3999                 op->o_bd = be;
4000                 op->o_callback = sc.sc_next;
4001                 op->o_dn = dn;
4002                 op->o_ndn = ndn;
4003         }
4004
4005         ldap_pvt_thread_pool_resume( &connection_pool );
4006 out:
4007         send_ldap_result( op, rs );
4008         return rs->sr_err;
4009 }
4010
4011 static int
4012 config_back_modrdn( Operation *op, SlapReply *rs )
4013 {
4014         CfBackInfo *cfb;
4015         CfEntryInfo *ce, *last;
4016
4017         if ( !be_isroot( op ) ) {
4018                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
4019                 goto out;
4020         }
4021
4022         cfb = (CfBackInfo *)op->o_bd->be_private;
4023
4024         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
4025         if ( !ce ) {
4026                 if ( last )
4027                         rs->sr_matched = last->ce_entry->e_name.bv_val;
4028                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
4029                 goto out;
4030         }
4031
4032         /* We don't allow moving objects to new parents.
4033          * Generally we only allow reordering a set of ordered entries.
4034          */
4035         if ( op->orr_newSup ) {
4036                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
4037                 goto out;
4038         }
4039         ldap_pvt_thread_pool_pause( &connection_pool );
4040
4041         ldap_pvt_thread_pool_resume( &connection_pool );
4042 out:
4043         send_ldap_result( op, rs );
4044         return rs->sr_err;
4045 }
4046
4047 static int
4048 config_back_search( Operation *op, SlapReply *rs )
4049 {
4050         CfBackInfo *cfb;
4051         CfEntryInfo *ce, *last;
4052
4053         if ( !be_isroot( op ) ) {
4054                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
4055                 goto out;
4056         }
4057
4058         cfb = (CfBackInfo *)op->o_bd->be_private;
4059
4060         ce = config_find_base( cfb->cb_root, &op->o_req_ndn, &last );
4061         if ( !ce ) {
4062                 if ( last )
4063                         rs->sr_matched = last->ce_entry->e_name.bv_val;
4064                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
4065                 goto out;
4066         }
4067         switch ( op->ors_scope ) {
4068         case LDAP_SCOPE_BASE:
4069         case LDAP_SCOPE_SUBTREE:
4070                 config_send( op, rs, ce, 0 );
4071                 break;
4072                 
4073         case LDAP_SCOPE_ONELEVEL:
4074                 for (ce = ce->ce_kids; ce; ce=ce->ce_sibs) {
4075                         config_send( op, rs, ce, 1 );
4076                 }
4077                 break;
4078         }
4079                 
4080         rs->sr_err = LDAP_SUCCESS;
4081 out:
4082         send_ldap_result( op, rs );
4083         return 0;
4084 }
4085
4086 static void
4087 config_build_attrs( Entry *e, AttributeType **at, AttributeDescription *ad,
4088         ConfigTable *ct, ConfigArgs *c )
4089 {
4090         int i, rc;
4091
4092         for (; at && *at; at++) {
4093                 /* Skip the naming attr */
4094                 if ((*at)->sat_ad == ad || (*at)->sat_ad == slap_schema.si_ad_cn )
4095                         continue;
4096                 for (i=0;ct[i].name;i++) {
4097                         if (ct[i].ad == (*at)->sat_ad) {
4098                                 rc = config_get_vals(&ct[i], c);
4099                                 if (rc == LDAP_SUCCESS) {
4100                                         if ( c->rvalue_nvals )
4101                                                 attr_merge(e, ct[i].ad, c->rvalue_vals,
4102                                                         c->rvalue_nvals);
4103                                         else
4104                                                 attr_merge_normalize(e, ct[i].ad,
4105                                                         c->rvalue_vals, NULL);
4106                                         ber_bvarray_free( c->rvalue_nvals );
4107                                         ber_bvarray_free( c->rvalue_vals );
4108                                 }
4109                                 break;
4110                         }
4111                 }
4112         }
4113 }
4114
4115 Entry *
4116 config_build_entry( Operation *op, SlapReply *rs, CfEntryInfo *parent,
4117         ConfigArgs *c, struct berval *rdn, ConfigOCs *main, ConfigOCs *extra )
4118 {
4119         Entry *e = ch_calloc( 1, sizeof(Entry) );
4120         CfEntryInfo *ce = ch_calloc( 1, sizeof(CfEntryInfo) );
4121         struct berval val;
4122         struct berval ad_name;
4123         AttributeDescription *ad = NULL;
4124         int rc;
4125         char *ptr;
4126         const char *text;
4127         Attribute *oc_at;
4128         struct berval pdn;
4129         ObjectClass *oc;
4130         CfEntryInfo *ceprev = NULL;
4131
4132         e->e_private = ce;
4133         ce->ce_entry = e;
4134         ce->ce_parent = parent;
4135         if ( parent ) {
4136                 pdn = parent->ce_entry->e_nname;
4137                 if ( parent->ce_kids )
4138                         for ( ceprev = parent->ce_kids; ceprev->ce_sibs;
4139                                 ceprev = ceprev->ce_sibs );
4140         } else {
4141                 BER_BVZERO( &pdn );
4142         }
4143
4144         ce->ce_type = main->co_type;
4145         ce->ce_private = c->private;
4146         ce->ce_be = c->be;
4147         ce->ce_bi = c->bi;
4148
4149         build_new_dn( &e->e_name, &pdn, rdn, NULL );
4150         ber_dupbv( &e->e_nname, &e->e_name );
4151
4152         attr_merge_normalize_one(e, slap_schema.si_ad_objectClass,
4153                 main->co_name, NULL );
4154         if ( extra )
4155                 attr_merge_normalize_one(e, slap_schema.si_ad_objectClass,
4156                         extra->co_name, NULL );
4157         ptr = strchr(rdn->bv_val, '=');
4158         ad_name.bv_val = rdn->bv_val;
4159         ad_name.bv_len = ptr - rdn->bv_val;
4160         rc = slap_bv2ad( &ad_name, &ad, &text );
4161         if ( rc ) {
4162                 return NULL;
4163         }
4164         val.bv_val = ptr+1;
4165         val.bv_len = rdn->bv_len - (val.bv_val - rdn->bv_val);
4166         attr_merge_normalize_one(e, ad, &val, NULL );
4167
4168         oc = main->co_oc;
4169         if ( oc->soc_required )
4170                 config_build_attrs( e, oc->soc_required, ad, main->co_table, c );
4171
4172         if ( oc->soc_allowed )
4173                 config_build_attrs( e, oc->soc_allowed, ad, main->co_table, c );
4174
4175         if ( extra ) {
4176                 oc = extra->co_oc;
4177                 if ( oc->soc_required )
4178                         config_build_attrs( e, oc->soc_required, ad, extra->co_table, c );
4179
4180                 if ( oc->soc_allowed )
4181                         config_build_attrs( e, oc->soc_allowed, ad, extra->co_table, c );
4182         }
4183
4184         oc_at = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
4185         rc = structural_class(oc_at->a_vals, &val, NULL, &text, c->msg,
4186                 sizeof(c->msg));
4187         attr_merge_normalize_one(e, slap_schema.si_ad_structuralObjectClass, &val, NULL );
4188         if ( op ) {
4189                 op->ora_e = e;
4190                 op->o_bd->be_add( op, rs );
4191         }
4192         if ( ceprev ) {
4193                 ceprev->ce_sibs = ce;
4194         } else if ( parent ) {
4195                 parent->ce_kids = ce;
4196         }
4197
4198         return e;
4199 }
4200
4201 static void
4202 config_build_schema_inc( ConfigArgs *c, CfEntryInfo *ceparent,
4203         Operation *op, SlapReply *rs )
4204 {
4205         Entry *e;
4206         ConfigFile *cf = c->private;
4207         char *ptr;
4208         struct berval bv;
4209
4210         for (; cf; cf=cf->c_sibs, c->depth++) {
4211                 c->value_dn.bv_val = c->log;
4212                 bv.bv_val = strrchr(cf->c_file.bv_val, LDAP_DIRSEP[0]);
4213                 if ( !bv.bv_val ) {
4214                         bv = cf->c_file;
4215                 } else {
4216                         bv.bv_val++;
4217                         bv.bv_len = cf->c_file.bv_len - (bv.bv_val - cf->c_file.bv_val);
4218                 }
4219                 ptr = strchr( bv.bv_val, '.' );
4220                 if ( ptr )
4221                         bv.bv_len = ptr - bv.bv_val;
4222                 c->value_dn.bv_len = snprintf(c->value_dn.bv_val, sizeof( c->log ), "cn=" SLAP_X_ORDERED_FMT, c->depth);
4223                 if ( c->value_dn.bv_len >= sizeof( c->log ) ) {
4224                         /* FIXME: how can indicate error? */
4225                         return;
4226                 }
4227                 strncpy( c->value_dn.bv_val + c->value_dn.bv_len, bv.bv_val,
4228                         bv.bv_len );
4229                 c->value_dn.bv_len += bv.bv_len;
4230                 c->value_dn.bv_val[c->value_dn.bv_len] ='\0';
4231
4232                 c->private = cf;
4233                 e = config_build_entry( op, rs, ceparent, c, &c->value_dn,
4234                         &CFOC_SCHEMA, NULL );
4235                 if ( e && cf->c_kids ) {
4236                         c->private = cf->c_kids;
4237                         config_build_schema_inc( c, e->e_private, op, rs );
4238                 }
4239         }
4240 }
4241
4242 static void
4243 config_build_includes( ConfigArgs *c, CfEntryInfo *ceparent,
4244         Operation *op, SlapReply *rs )
4245 {
4246         Entry *e;
4247         int i;
4248         ConfigFile *cf = c->private;
4249
4250         for (i=0; cf; cf=cf->c_sibs, i++) {
4251                 c->value_dn.bv_val = c->log;
4252                 c->value_dn.bv_len = snprintf(c->value_dn.bv_val, sizeof( c->log ), "cn=include" SLAP_X_ORDERED_FMT, i);
4253                 if ( c->value_dn.bv_len >= sizeof( c->log ) ) {
4254                         /* FIXME: how can indicate error? */
4255                         return;
4256                 }
4257                 c->private = cf;
4258                 e = config_build_entry( op, rs, ceparent, c, &c->value_dn,
4259                         &CFOC_INCLUDE, NULL );
4260                 if ( e && cf->c_kids ) {
4261                         c->private = cf->c_kids;
4262                         config_build_includes( c, e->e_private, op, rs );
4263                 }
4264         }
4265 }
4266
4267 #ifdef SLAPD_MODULES
4268
4269 static void
4270 config_build_modules( ConfigArgs *c, CfEntryInfo *ceparent,
4271         Operation *op, SlapReply *rs )
4272 {
4273         int i;
4274         ModPaths *mp;
4275
4276         for (i=0, mp=&modpaths; mp; mp=mp->mp_next, i++) {
4277                 if ( BER_BVISNULL( &mp->mp_path ) && !mp->mp_loads )
4278                         continue;
4279                 c->value_dn.bv_val = c->log;
4280                 c->value_dn.bv_len = snprintf(c->value_dn.bv_val, sizeof( c->log ), "cn=module" SLAP_X_ORDERED_FMT, i);
4281                 if ( c->value_dn.bv_len >= sizeof( c->log ) ) {
4282                         /* FIXME: how can indicate error? */
4283                         return;
4284                 }
4285                 c->private = mp;
4286                 config_build_entry( op, rs, ceparent, c, &c->value_dn,
4287                         &CFOC_MODULE, NULL );
4288         }
4289 }
4290 #endif
4291
4292 static int
4293 config_back_db_open( BackendDB *be )
4294 {
4295         CfBackInfo *cfb = be->be_private;
4296         struct berval rdn;
4297         Entry *e, *parent;
4298         CfEntryInfo *ce, *ceparent;
4299         int i, unsupp = 0;
4300         BackendInfo *bi;
4301         ConfigArgs c;
4302         Connection conn = {0};
4303         OperationBuffer opbuf;
4304         Operation *op;
4305         slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
4306         SlapReply rs = {REP_RESULT};
4307         void *thrctx = NULL;
4308
4309         /* If we read the config from back-ldif, nothing to do here */
4310         if ( cfb->cb_got_ldif )
4311                 return 0;
4312
4313         if ( cfb->cb_use_ldif ) {
4314                 thrctx = ldap_pvt_thread_pool_context();
4315                 op = (Operation *) &opbuf;
4316                 connection_fake_init( &conn, op, thrctx );
4317
4318                 op->o_tag = LDAP_REQ_ADD;
4319                 op->o_callback = &cb;
4320                 op->o_bd = &cfb->cb_db;
4321                 op->o_dn = op->o_bd->be_rootdn;
4322                 op->o_ndn = op->o_bd->be_rootndn;
4323         } else {
4324                 op = NULL;
4325         }
4326
4327         /* create root of tree */
4328         rdn = config_rdn;
4329         c.private = cfb->cb_config;
4330         c.be = frontendDB;
4331         e = config_build_entry( op, &rs, NULL, &c, &rdn, &CFOC_GLOBAL, NULL );
4332         ce = e->e_private;
4333         cfb->cb_root = ce;
4334
4335         parent = e;
4336         ceparent = ce;
4337
4338         /* Create includeFile nodes */
4339         if ( cfb->cb_config->c_kids ) {
4340                 c.depth = 0;
4341                 c.private = cfb->cb_config->c_kids;
4342                 config_build_includes( &c, ceparent, op, &rs );
4343         }
4344
4345 #ifdef SLAPD_MODULES
4346         /* Create Module nodes... */
4347         if ( modpaths.mp_loads ) {
4348                 config_build_modules( &c, ceparent, op, &rs );
4349         }
4350 #endif
4351
4352         /* Create schema nodes... cn=schema will contain the hardcoded core
4353          * schema, read-only. Child objects will contain runtime loaded schema
4354          * files.
4355          */
4356         rdn = schema_rdn;
4357         c.private = NULL;
4358         e = config_build_entry( op, &rs, ceparent, &c, &rdn, &CFOC_SCHEMA, NULL );
4359         ce = e->e_private;
4360
4361         /* Create schema nodes for included schema... */
4362         if ( cfb->cb_config->c_kids ) {
4363                 c.depth = 0;
4364                 c.private = cfb->cb_config->c_kids;
4365                 config_build_schema_inc( &c, ce, op, &rs );
4366         }
4367
4368         /* Create backend nodes. Skip if they don't provide a cf_table.
4369          * There usually aren't any of these.
4370          */
4371         
4372         c.line = 0;
4373         LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next) {
4374                 if (!bi->bi_cf_ocs) {
4375                         /* If it only supports the old config mech, complain. */
4376                         if ( bi->bi_config ) {
4377                                 Debug( LDAP_DEBUG_ANY,
4378                                         "WARNING: No dynamic config support for backend %s.\n",
4379                                         bi->bi_type, 0, 0 );
4380                                 unsupp++;
4381                         }
4382                         continue;
4383                 }
4384                 if (!bi->bi_private) continue;
4385
4386                 rdn.bv_val = c.log;
4387                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( c.log ),
4388                         "%s=%s", cfAd_backend->ad_cname.bv_val, bi->bi_type);
4389                 if ( rdn.bv_len >= sizeof( c.log ) ) {
4390                         /* FIXME: holler ... */ ;
4391                 }
4392                 c.bi = bi;
4393                 e = config_build_entry( op, &rs, ceparent, &c, &rdn, &CFOC_BACKEND,
4394                         bi->bi_cf_ocs );
4395         }
4396
4397         /* Create database nodes... */
4398         frontendDB->be_cf_ocs = &CFOC_FRONTEND;
4399         LDAP_STAILQ_NEXT(frontendDB, be_next) = LDAP_STAILQ_FIRST(&backendDB);
4400         for ( i = -1, be = frontendDB ; be;
4401                 i++, be = LDAP_STAILQ_NEXT( be, be_next )) {
4402                 slap_overinfo *oi = NULL;
4403
4404                 if ( overlay_is_over( be )) {
4405                         oi = be->bd_info->bi_private;
4406                         bi = oi->oi_orig;
4407                 } else {
4408                         bi = be->bd_info;
4409                 }
4410
4411                 /* If this backend supports the old config mechanism, but not
4412                  * the new mech, complain.
4413                  */
4414                 if ( !be->be_cf_ocs && bi->bi_db_config ) {
4415                         Debug( LDAP_DEBUG_ANY,
4416                                 "WARNING: No dynamic config support for database %s.\n",
4417                                 bi->bi_type, 0, 0 );
4418                         unsupp++;
4419                 }
4420                 rdn.bv_val = c.log;
4421                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( c.log ),
4422                         "%s=" SLAP_X_ORDERED_FMT "%s", cfAd_database->ad_cname.bv_val,
4423                         i, bi->bi_type);
4424                 if ( rdn.bv_len >= sizeof( c.log ) ) {
4425                         /* FIXME: holler ... */ ;
4426                 }
4427                 c.be = be;
4428                 c.bi = bi;
4429                 e = config_build_entry( op, &rs, ceparent, &c, &rdn, &CFOC_DATABASE,
4430                         be->be_cf_ocs );
4431                 ce = e->e_private;
4432                 if ( be->be_cf_ocs && be->be_cf_ocs->co_cfadd )
4433                         be->be_cf_ocs->co_cfadd( op, &rs, e, &c );
4434                 /* Iterate through overlays */
4435                 if ( oi ) {
4436                         slap_overinst *on;
4437                         Entry *oe;
4438                         int j;
4439
4440                         for (j=0,on=oi->oi_list; on; j++,on=on->on_next) {
4441                                 if ( on->on_bi.bi_db_config && !on->on_bi.bi_cf_ocs ) {
4442                                         Debug( LDAP_DEBUG_ANY,
4443                                                 "WARNING: No dynamic config support for overlay %s.\n",
4444                                                 on->on_bi.bi_type, 0, 0 );
4445                                         unsupp++;
4446                                 }
4447                                 rdn.bv_val = c.log;
4448                                 rdn.bv_len = snprintf(rdn.bv_val, sizeof( c.log ),
4449                                         "%s=" SLAP_X_ORDERED_FMT "%s",
4450                                         cfAd_overlay->ad_cname.bv_val, j, on->on_bi.bi_type );
4451                                 if ( rdn.bv_len >= sizeof( c.log ) ) {
4452                                         /* FIXME: holler ... */ ;
4453                                 }
4454                                 c.be = be;
4455                                 c.bi = &on->on_bi;
4456                                 oe = config_build_entry( op, &rs, ce, &c, &rdn,
4457                                         &CFOC_OVERLAY, c.bi->bi_cf_ocs );
4458                                 if ( c.bi->bi_cf_ocs && c.bi->bi_cf_ocs->co_cfadd )
4459                                         c.bi->bi_cf_ocs->co_cfadd( op, &rs, oe, &c );
4460                         }
4461                 }
4462         }
4463         if ( thrctx )
4464                 ldap_pvt_thread_pool_context_reset( thrctx );
4465
4466         if ( unsupp  && cfb->cb_use_ldif ) {
4467                 Debug( LDAP_DEBUG_ANY, "\nWARNING: The converted cn=config "
4468                         "directory is incomplete and may not work.\n\n", 0, 0, 0 );
4469         }
4470
4471         return 0;
4472 }
4473
4474 static void
4475 cfb_free_cffile( ConfigFile *cf )
4476 {
4477         ConfigFile *next;
4478
4479         for (; cf; cf=next) {
4480                 next = cf->c_sibs;
4481                 if ( cf->c_kids )
4482                         cfb_free_cffile( cf->c_kids );
4483                 ch_free( cf->c_file.bv_val );
4484                 ber_bvarray_free( cf->c_dseFiles );
4485                 ch_free( cf );
4486         }
4487 }
4488
4489 static void
4490 cfb_free_entries( CfEntryInfo *ce )
4491 {
4492         CfEntryInfo *next;
4493
4494         for (; ce; ce=next) {
4495                 next = ce->ce_sibs;
4496                 if ( ce->ce_kids )
4497                         cfb_free_entries( ce->ce_kids );
4498                 ce->ce_entry->e_private = NULL;
4499                 entry_free( ce->ce_entry );
4500                 ch_free( ce );
4501         }
4502 }
4503
4504 static int
4505 config_back_db_close( BackendDB *be )
4506 {
4507         CfBackInfo *cfb = be->be_private;
4508
4509         cfb_free_entries( cfb->cb_root );
4510         cfb->cb_root = NULL;
4511
4512         if ( cfb->cb_db.bd_info ) {
4513                 backend_shutdown( &cfb->cb_db );
4514         }
4515
4516         return 0;
4517 }
4518
4519 static int
4520 config_back_db_destroy( BackendDB *be )
4521 {
4522         CfBackInfo *cfb = be->be_private;
4523
4524         cfb_free_cffile( cfb->cb_config );
4525
4526         ch_free( cfdir.bv_val );
4527
4528         avl_free( CfOcTree, NULL );
4529
4530         if ( cfb->cb_db.bd_info ) {
4531                 cfb->cb_db.be_suffix = NULL;
4532                 cfb->cb_db.be_nsuffix = NULL;
4533                 BER_BVZERO( &cfb->cb_db.be_rootdn );
4534                 BER_BVZERO( &cfb->cb_db.be_rootndn );
4535
4536                 backend_destroy_one( &cfb->cb_db, 0 );
4537         }
4538
4539         free( be->be_private );
4540
4541         loglevel_destroy();
4542
4543         return 0;
4544 }
4545
4546 static int
4547 config_back_db_init( BackendDB *be )
4548 {
4549         struct berval dn;
4550         CfBackInfo *cfb;
4551
4552         cfb = ch_calloc( 1, sizeof(CfBackInfo));
4553         cfb->cb_config = ch_calloc( 1, sizeof(ConfigFile));
4554         cfn = cfb->cb_config;
4555         be->be_private = cfb;
4556
4557         ber_dupbv( &be->be_rootdn, &config_rdn );
4558         ber_dupbv( &be->be_rootndn, &be->be_rootdn );
4559         ber_dupbv( &dn, &be->be_rootdn );
4560         ber_bvarray_add( &be->be_suffix, &dn );
4561         ber_dupbv( &dn, &be->be_rootdn );
4562         ber_bvarray_add( &be->be_nsuffix, &dn );
4563
4564         /* Hide from namingContexts */
4565         SLAP_BFLAGS(be) |= SLAP_BFLAG_CONFIG;
4566
4567         return 0;
4568 }
4569
4570 static int
4571 config_back_destroy( BackendInfo *bi )
4572 {
4573         ldif_must_b64_encode_release();
4574         return 0;
4575 }
4576
4577 static int
4578 config_tool_entry_open( BackendDB *be, int mode )
4579 {
4580         CfBackInfo *cfb = be->be_private;
4581         BackendInfo *bi = cfb->cb_db.bd_info;
4582
4583         if ( bi && bi->bi_tool_entry_open )
4584                 return bi->bi_tool_entry_open( &cfb->cb_db, mode );
4585         else
4586                 return -1;
4587         
4588 }
4589
4590 static int
4591 config_tool_entry_close( BackendDB *be )
4592 {
4593         CfBackInfo *cfb = be->be_private;
4594         BackendInfo *bi = cfb->cb_db.bd_info;
4595
4596         if ( bi && bi->bi_tool_entry_close )
4597                 return bi->bi_tool_entry_close( &cfb->cb_db );
4598         else
4599                 return -1;
4600 }
4601
4602 static ID
4603 config_tool_entry_first( BackendDB *be )
4604 {
4605         CfBackInfo *cfb = be->be_private;
4606         BackendInfo *bi = cfb->cb_db.bd_info;
4607
4608         if ( bi && bi->bi_tool_entry_first )
4609                 return bi->bi_tool_entry_first( &cfb->cb_db );
4610         else
4611                 return NOID;
4612 }
4613
4614 static ID
4615 config_tool_entry_next( BackendDB *be )
4616 {
4617         CfBackInfo *cfb = be->be_private;
4618         BackendInfo *bi = cfb->cb_db.bd_info;
4619
4620         if ( bi && bi->bi_tool_entry_next )
4621                 return bi->bi_tool_entry_next( &cfb->cb_db );
4622         else
4623                 return NOID;
4624 }
4625
4626 static Entry *
4627 config_tool_entry_get( BackendDB *be, ID id )
4628 {
4629         CfBackInfo *cfb = be->be_private;
4630         BackendInfo *bi = cfb->cb_db.bd_info;
4631
4632         if ( bi && bi->bi_tool_entry_get )
4633                 return bi->bi_tool_entry_get( &cfb->cb_db, id );
4634         else
4635                 return NULL;
4636 }
4637
4638 static ID
4639 config_tool_entry_put( BackendDB *be, Entry *e, struct berval *text )
4640 {
4641         CfBackInfo *cfb = be->be_private;
4642         BackendInfo *bi = cfb->cb_db.bd_info;
4643         ConfigArgs ca;
4644
4645         if ( bi && bi->bi_tool_entry_put &&
4646                 config_add_internal( cfb, e, &ca, NULL, NULL ) == 0 )
4647                 return bi->bi_tool_entry_put( &cfb->cb_db, e, text );
4648         else
4649                 return NOID;
4650 }
4651
4652 static struct {
4653         char *name;
4654         AttributeDescription **desc;
4655 } ads[] = {
4656         { "backend", &cfAd_backend },
4657         { "database", &cfAd_database },
4658         { "include", &cfAd_include },
4659         { "overlay", &cfAd_overlay },
4660         { NULL, NULL }
4661 };
4662
4663 /* Notes:
4664  *   add / delete: all types that may be added or deleted must use an
4665  * X-ORDERED attributeType for their RDN. Adding and deleting entries
4666  * should automatically renumber the index of any siblings as needed,
4667  * so that no gaps in the numbering sequence exist after the add/delete
4668  * is completed.
4669  *   What can be added:
4670  *     schema objects
4671  *     backend objects for backend-specific config directives
4672  *     database objects
4673  *     overlay objects
4674  *
4675  *   delete: probably no support this time around.
4676  *
4677  *   modrdn: generally not done. Will be invoked automatically by add/
4678  * delete to update numbering sequence. Perform as an explicit operation
4679  * so that the renumbering effect may be replicated. Subtree rename must
4680  * be supported, since renumbering a database will affect all its child
4681  * overlays.
4682  *
4683  *  modify: must be fully supported. 
4684  */
4685
4686 int
4687 config_back_initialize( BackendInfo *bi )
4688 {
4689         ConfigTable             *ct = config_back_cf_table;
4690         char                    *argv[4];
4691         int                     i;
4692         AttributeDescription    *ad = NULL;
4693         const char              *text;
4694         static char             *controls[] = {
4695                 LDAP_CONTROL_MANAGEDSAIT,
4696                 NULL
4697         };
4698
4699         bi->bi_controls = controls;
4700
4701         bi->bi_open = 0;
4702         bi->bi_close = 0;
4703         bi->bi_config = 0;
4704         bi->bi_destroy = config_back_destroy;
4705
4706         bi->bi_db_init = config_back_db_init;
4707         bi->bi_db_config = 0;
4708         bi->bi_db_open = config_back_db_open;
4709         bi->bi_db_close = config_back_db_close;
4710         bi->bi_db_destroy = config_back_db_destroy;
4711
4712         bi->bi_op_bind = config_back_bind;
4713         bi->bi_op_unbind = 0;
4714         bi->bi_op_search = config_back_search;
4715         bi->bi_op_compare = 0;
4716         bi->bi_op_modify = config_back_modify;
4717         bi->bi_op_modrdn = config_back_modrdn;
4718         bi->bi_op_add = config_back_add;
4719         bi->bi_op_delete = 0;
4720         bi->bi_op_abandon = 0;
4721
4722         bi->bi_extended = 0;
4723
4724         bi->bi_chk_referrals = 0;
4725
4726 #ifdef SLAP_OVERLAY_ACCESS
4727         bi->bi_access_allowed = slap_access_always_allowed;
4728 #endif /* SLAP_OVERLAY_ACCESS */
4729
4730         bi->bi_connection_init = 0;
4731         bi->bi_connection_destroy = 0;
4732
4733         bi->bi_tool_entry_open = config_tool_entry_open;
4734         bi->bi_tool_entry_close = config_tool_entry_close;
4735         bi->bi_tool_entry_first = config_tool_entry_first;
4736         bi->bi_tool_entry_next = config_tool_entry_next;
4737         bi->bi_tool_entry_get = config_tool_entry_get;
4738         bi->bi_tool_entry_put = config_tool_entry_put;
4739
4740         /* Make sure we don't exceed the bits reserved for userland */
4741         assert( ( ( CFG_LAST - 1 ) & ARGS_USERLAND ) == ( CFG_LAST - 1 ) );
4742
4743         argv[3] = NULL;
4744         for (i=0; OidMacros[i].name; i++ ) {
4745                 argv[1] = OidMacros[i].name;
4746                 argv[2] = OidMacros[i].oid;
4747                 parse_oidm( "slapd", i, 3, argv, 0, NULL );
4748         }
4749
4750         bi->bi_cf_ocs = cf_ocs;
4751
4752         i = config_register_schema( ct, cf_ocs );
4753         if ( i ) return i;
4754
4755         /* setup olcRootPW to be base64-encoded when written in LDIF form;
4756          * basically, we don't care if it fails */
4757         i = slap_str2ad( "olcRootPW", &ad, &text );
4758         if ( i ) {
4759                 Debug( LDAP_DEBUG_ANY, "config_back_initialize: "
4760                         "warning, unable to get \"olcRootPW\" "
4761                         "attribute description: %d: %s\n",
4762                         i, text, 0 );
4763         } else {
4764                 (void)ldif_must_b64_encode_register( ad->ad_cname.bv_val,
4765                         ad->ad_type->sat_oid );
4766         }
4767
4768         /* set up the notable AttributeDescriptions */
4769         i = 0;
4770         for (;ct->name;ct++) {
4771                 if (strcmp(ct->name, ads[i].name)) continue;
4772                 *ads[i].desc = ct->ad;
4773                 i++;
4774                 if (!ads[i].name) break;
4775         }
4776
4777         return 0;
4778 }
4779