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