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